0

GenEdQuestion.class The mode of quiz is multiple choice. I want to save or transfer the correct answer and the chosen answer to another activity (result). I transferred the score but i don't know how to transfer the result. How can I do that?

      answer1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (answer1.getText() == mAnswer){
                mNumber++;
                mScore++;
                number.setText(" "+mNumber+" .");
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }
            }else{
                mNumber++;
                number.setText(" "+mNumber+" .");
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }
            }
            return false;
        }
    });

    answer2.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (answer2.getText() == mAnswer){
                mNumber++;
                number.setText(" "+mNumber+" .");
                mScore++;
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }

            }else{
                mNumber++;
                number.setText(" "+mNumber+" .");
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }

            }
            return false;
        }
    });

    answer3.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (answer3.getText() == mAnswer){
                mNumber++;
                number.setText(" "+mNumber+" .");
                mScore++;
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }

            }else{
                mNumber++;
                number.setText(" "+mNumber+" .");
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }

            }
            return false;
        }
    });

    answer4.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (answer4.getText() == mAnswer){
                mNumber++;
                number.setText(" "+mNumber+" .");
                mScore++;
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }

            }else{
                mNumber++;
                number.setText(" "+mNumber+" .");
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }
            }
            return false;
        }
    });


   new CountDownTimer(60000*60, 1000){
        public void onTick(long secondsLeft){
            mCountDownGenEd.setText(""+String.format("%d min, %d sec",
                    TimeUnit.MILLISECONDS.toMinutes(secondsLeft),
                    TimeUnit.MILLISECONDS.toSeconds(secondsLeft) -
            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(secondsLeft))));

        }
        public void onFinish(){
            Intent intent = new Intent(getApplicationContext(), GenEdQuestionsResult.class);
            intent.putExtra("this", mScore);
            startActivity(intent);
        }
    }.start();
}

private void onFinish() {
    Intent intent = new Intent(getApplicationContext(), GenEdQuestionsResult.class);
    intent.putExtra("this", mScore);
    startActivity(intent);
}

private void updateQuestion(int i) {

    question.setText(mQuestions.getQuestion(i));
    answer1.setText(mQuestions.getChoice1(i));
    answer2.setText(mQuestions.getChoice2(i));
    answer3.setText(mQuestions.getChoice3(i));
    answer4.setText(mQuestions.getChoice4(i));
    mAnswer = mQuestions.getCorrectAnswer(i);

}

GenEdQuestionsResult.class This activity will show the score and if the user passed or failed. How will i put the result here?

    mFinalScore = (TextView) findViewById(R.id.scoreTextView);
    mResult = (TextView) findViewById(R.id.resultTextView);
    int score = getIntent().getIntExtra("this", 0);
    mFinalScore.setText(score+" / 20");
    if (score <= 10){mResult.setText("Failed");}
    else{mResult.setText("Passed");}

This is the result i want to create

this is the picture of result i want to create

  • 2
    That's a lot of code. What exactly are you stuck on for passing the result? You just pass another extra through the intent and retrieve it as you do with the score. – codeMagic Oct 24 '17 at 00:45
  • @codeMagic i want to show the chosen answer and the correct answer in another activity. I tried the another extra but it showing null value – Christian Manansala Oct 24 '17 at 09:51
  • Ok, but you don't show how you try to send and receive it so we can't tell you what you did wrong. – codeMagic Oct 24 '17 at 12:46
  • I just added this few lines of codes (**Main**) ----- private void onFinish() { Intent intent = new Intent(getApplicationContext(), GenEdQuestionsResult.class); intent.putExtra("this", mScore); intent.putExtra("answer", mAnswer); startActivity(intent); } (**GenEdQuestionResult**) ---- int score = getIntent().getIntExtra("this", 0); String answer = getIntent().getStringExtra("answer"); mFinalScore.setText(score+" / 50"); mAnswer.setText(answer); _It shows 'null' value in result activity_ – Christian Manansala Oct 24 '17 at 13:07
  • That needs to be edited into your post. Reading lines of code and errors is a pain in comments. Use the edit button below the post. Also, tell us what is null and/or post the complete error message. – codeMagic Oct 24 '17 at 13:52

2 Answers2

1

you have 3 approaches to do this the first one=> make a static variable and then you can access it where ever you want ,

the second one you can => use intent putExtra

Intent i = new Intent(this, SecondClass.class);
i.putExtra("answer", "myAnswer");
startActivity(i)

the third approach is using a sharedPreference take a look at this link https://developer.android.com/reference/android/content/SharedPreferences.html

Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34
0

Use shared preference like this:

public static final String answer = "answer"; //declare this globally at the top.
SharedPreferences prefs = getSharedPreferences(Commonclass.ANSWER,Context.MODE_PRIVATE);
SharedPreferences.Editor editor;
editor = prefs.edit();
String a = prefs.getString("answer","answer"); //to get the stored value
editor.putString(theme,"blue"); // to store your answer
editor.commit();

Create a Commonclass:

public class Commonclass 
{
  public static final String ANSWER = "answer" ;
}
Rakesh Polo
  • 431
  • 1
  • 9
  • 27