LooseLeaf224
LooseLeaf224

Reputation: 11

Android studio new intent without a button

I am making an quiz app for my class that asks 10 questions and then give the results after the last question.

I have been able to set up the quiz no problem, but I can not get the Intent to work to go to the next screen to show my score. I have no idea if I've gone crazy or if I have been staring at my computer for too long today.

public class MainActivity extends AppCompatActivity {

private QuestionLibrary mQuestionLibrary = new QuestionLibrary();


private TextView mQuestionView;
private Button mButtonChoice1;
private Button mButtonChoice2;
private Button mButtonChoice3;

private String mAnswer;
private int mScore = 0;
private int mQuestionNumber = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mQuestionView = (TextView) findViewById(R.id.question);
    mButtonChoice1 = (Button) findViewById(R.id.button1);
    mButtonChoice2 = (Button) findViewById(R.id.button2);
    mButtonChoice3 = (Button) findViewById(R.id.button3);

    updateQuestion();

    mButtonChoice1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mButtonChoice1.getText() == mAnswer) {
                mScore = mScore + 1;

                updateQuestion();
            } else {
                updateQuestion();
            }
        }
    });

    mButtonChoice2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mButtonChoice2.getText() == mAnswer) {
                mScore = mScore + 1;

                updateQuestion();
            } else {
                updateQuestion();
            }
        }
    });

    mButtonChoice3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mButtonChoice3.getText() == mAnswer) {
                mScore = mScore + 1;

                updateQuestion();
            } else {
                updateQuestion();
            }

        }
    });

    Intent intent = new Intent(this, ResultActivity.class);
    startActivity(intent);

}

public void setScore(int score) {
    mScore = score;
}

public int getScore() {
    return mScore;
}

private void updateQuestion() {
    mQuestionView.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
    mButtonChoice1.setText(mQuestionLibrary.getChoice1(mQuestionNumber));
    mButtonChoice2.setText(mQuestionLibrary.getChoice2(mQuestionNumber));
    mButtonChoice3.setText(mQuestionLibrary.getChoice3(mQuestionNumber));

    mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber);
    mQuestionNumber++;

}

}

Then my result activity where I should be displaying the final score.

public class ResultActivity extends MainActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
}

public void results(View view) {

    TextView results = (TextView) findViewById(R.id.score);


    results.setText(getScore());


}

}

Upvotes: 0

Views: 1452

Answers (4)

I think you should try this.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          //Created  a object of handler
            final Handler handler = new Handler();
            //For moving to next activity after sometime.
            new Handler().postDelayed(new Runnable(){
                public void run(){
                    //To go to next activity
                    Intent page1 = new Intent(MainActivity.this, Start.class);
                            startActivity(page1);
                }
            }, 500); //Time in milliseconds in which new window will appear.
        }
    }

Upvotes: 0

Kiran Benny Joseph
Kiran Benny Joseph

Reputation: 6813

You should get intent in next activity like this.And should pass values like this

Intent intent = new Intent(this, ResultActivity.class);
intent.putExtra("score",mscore);
startActivity(intent);

for getting try this in ResultActivity

Intent intent=getIntent();
results.setText(intent.getIntExtra("score",0));

It is because you are not passing any values to next activity. Refer this. And you are making a start intent (Intent produced in on creation activity) which will not pass your result to next answer. You should start activity after game is over.

Upvotes: 0

Purvil Bambharolia
Purvil Bambharolia

Reputation: 681

The problem with the above code is that you are creating an intent to start the ResultActivity as soon as you are launching the MainActivity. My suggestion is to create the intent to change the activity when the quiz is over. You can do that by making a function and calling that function when the quiz is over or the counter of the questions is equal to 10 in your case. Moreover, you can declare a static variable in the MainActivity named score which can be accessed by the ResultActivity without making an object of MainActivity.

Something like this -

public void changeActivity(){
   Intent intent = new Intent(this, ResultActivity.class);
   startActivity(intent);
}

Upvotes: 1

Tixeon
Tixeon

Reputation: 930

move this from MainActivity's onCreate() method

Intent intent = new Intent(this, ResultActivity.class);
startActivity(intent);

to

private void updateQuestion() {
    mQuestionView.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
    mButtonChoice1.setText(mQuestionLibrary.getChoice1(mQuestionNumber));
    mButtonChoice2.setText(mQuestionLibrary.getChoice2(mQuestionNumber));
    mButtonChoice3.setText(mQuestionLibrary.getChoice3(mQuestionNumber));

    mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber);
    mQuestionNumber++;

    Intent intent = new Intent(this, ResultActivity.class);
    startActivity(intent);

 }

Upvotes: 0

Related Questions