user465001
user465001

Reputation: 846

Creating android trivia game , cannot proceed to next quest. after it is correctly answered

1.first a random number is selected.

2.the random number corresponds to a case (trivia question) in a switch statement.

3.the case has an onClick() for every possible answer.

4.when the correct answer (and it's corresponding onClick) is selected I want the dice to roll again and progress the game to the next question.

5.My debug log says tells me the value of this dice rolled. However, I cannot get the view to change for the corresponding case in the switch.

`public class BeginGame extends Activity  {

Random generator = new Random();
private static final String TAG = "MyActivity";

boolean dupe = true;

boolean done = false;
int intitializedQuestionValue = -2;
final int rows = 3;
final int columns = 25; 
final int RIGHT = -2000;
final int WRONG = -1000;
int score = 0;
int num;

//define array of three rows and 25 columns
int[][] questionArray = new int [rows][columns];
int[] questionNumber = new int [25];

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);

    final Button button0 = (Button) findViewById(R.id.button0);
    final Button button1 = (Button) findViewById(R.id.button1);
    final Button button2 = (Button) findViewById(R.id.button2);
    final Button button3 = (Button) findViewById(R.id.button3);
    final TextView text = (TextView) findViewById(R.id.TextView01);

    //initialize "dice"
    for (int i=0; i<25; i++){
        questionNumber[i] = -1;
    }


    for(int i=0; i < columns; i++)
        questionArray[0][i] = intitializedQuestionValue;

    //set all questions to answered WRONG
    for (int i=0; i <columns; i++)
        questionArray[1][i] = WRONG; 

    rollDice();

    loop: while (!done) 

        switch (num) {
        case 0:            

            text.setText("press Virginia0:");

            button0.setText("Alabama");
            button0.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(BeginGame.this, "fail", Toast.LENGTH_SHORT).show(); rollDice(); 


                    //questionArray[2][0]= score -2;


                }

            });
            button1.setText("Mississippi");
            button1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Toast.makeText(BeginGame.this, "fail", Toast.LENGTH_SHORT).show(); rollDice();


                    //questionArray[2][0]= score - 2;

                }
            });


            button2.setText("Philadelphia");
            button2.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(BeginGame.this, "fail", Toast.LENGTH_SHORT).show(); rollDice();


                    //questionArray[2][0]= score -2;



                }
            });

            button3.setText("Virginia"); 
            button3.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(BeginGame.this, "success", Toast.LENGTH_SHORT).show(); rollDice();


                    //questionArray[1][0]=RIGHT;
                    //questionArray[2][0]= score + 5;



                }
            });


            break loop; 

        case 1:             

            text.setText("press alabama1:");

            button0.setText("Alabama");
            button0.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {
                    Toast.makeText(BeginGame.this, "success", Toast.LENGTH_SHORT).show(); rollDice();




                }
            });

            button1.setText("Mississippi");
            button1.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {


                }
            });

            button2.setText("Philadelphia");
            button2.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {


                }
            });

            button3.setText("Virginia"); 
            button3.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v) {


                }
            });


            break loop; 

`

Upvotes: 1

Views: 480

Answers (1)

Snicolas
Snicolas

Reputation: 38168

your program won't work this way. You have to understand more of android.

The onCreate method is called when an activity is created. ie : when it comes to front. It is executed on the UI thread : a thread that interacts with the user and should never be blocked. So, you are basically blocking the UI thread with your loop (does num ever change by the way ?).

What you should do is remove your while loop from the onCreate method. Just use it to initialize your activity, maybe data structures like questions and widgets and their listeners.

And now give more logic to your listeners : when a button is clicked then change your question and refresh the interface so that the new questions display. Do that until there is no more question to ask.

Do never block the UI Thread, make it free so users can use your app.

Upvotes: 2

Related Questions