0

I have a quiz application I am working on whereby the questions, answers and options are embedded in the application with a timer. I also have two other activities: one for showing the user his current score and the other to review the answered questions once there is a timeout. I intend to send the questions and answers the user has tried before the timer was over into my review activity for review. For example, since I have 10 questions in my application, and the user was able to try 5 out of them before the time elapsed, I need to get those 5 questions into my Review Activity for review.

QuestionLibrary.Java

// This file contains questions from QuestionBank
class QuestionLibrary {
    // array of questions
    private String mQuestions [] = {
          // Question goes here
   };
    // array of multiple choices for each question
    private String mChoices [][] = {
           // question choices goes here
    };

    // array of correct answers - in the same order as array of questions
private String mCorrectAnswers[] = {
         // answers appear here
};


    // method returns number of questions
    int getLength(){
        return mQuestions.length;
    }

    // method returns question from array textQuestions[] based on array index
    String getQuestion(int a) {
        return mQuestions[a];
    }

    // method returns a single multiple choice item for question-based on array index,
    // based on number of multiple choice item in the list - 1, 2, 3 or 4 as an argument
    String getChoice(int index, int num) {
        return mChoices[index][num-1];
    }

    //  method returns correct answer for the question based on array index
    String getCorrectAnswer(int a) {
        return mCorrectAnswers[a];
    }

}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private QuestionLibrary mQuestionLibrary = new QuestionLibrary();
    Button button1;
    Button button2;
    Button button3;
    Button button4;
    TextView msingleQuestion;
    TextView mQuestion; //current question to answer
    TextView timer;
   MainActivity.CounterClass count_timer;   //countdown timer class
    private int mQuestionNumber = 0; // current question number
    private String mAnswer;  // correct answer for question in mQuestionView
    private int mquizNumber = 1;
    private long resume_timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // set textViews here
        // Setting countdown timer
        timer = (TextView) findViewById(R.id.timer);
        timer.setText("00:5:00");
        // start timer here
        count_timer = new MainActivity.CounterClass(300000, 1000);
        count_timer.start();
        updateQuizNumber(mquizNumber);

    }

    private void updateQuizNumber(int mquizNumber) {
        msingleQuestion.setText("" + mquizNumber+"/"+mQuestionLibrary.getLength());
    }


    private void updateQuestion() {
        // check if we are not outside array bounds for questions
        if(mQuestionNumber<mQuestionLibrary.getLength() ){
            // set text for hint and click_me
            mClickMe.setText(mQuestionLibrary.getTextHint(mQuestionNumber));
            // set the text for new question, and new 4 alternative to answer on four buttons
            mQuestion.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
            button1.setText(mQuestionLibrary.getChoice(mQuestionNumber, 1));
            button2.setText(mQuestionLibrary.getChoice(mQuestionNumber, 2));
            button3.setText(mQuestionLibrary.getChoice(mQuestionNumber, 3));
            button4.setText(mQuestionLibrary.getChoice(mQuestionNumber,4));
            mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber);
            mQuestionNumber++;
        }
        else {
            Toast.makeText(BeginnerActivity.this, "That was the last question!", Toast.LENGTH_SHORT).show();
       startActivity(intent);
        }
    }

    public void onClick(View view) {
        //all logic for all answers buttons in one method

        if (mQuestionNumber < mQuestionLibrary.getLength()) {
            // once user answer the question, we move on to the next one, if any
                    updateQuestion();
                    mquizNumber++;
                    updateQuizNumber(mquizNumber);
                }

            }, 1000);


        } else {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(MainActivity.this, ResultsActivity.class);
                    finish();
                    count_timer.cancel();
                }

            }, 1000);

        }

    @Override
    protected void onRestart() {
        //resume timer when user comes back to the app
        count_timer = new BeginnerActivity.CounterClass(resume_timer, 1000);
        count_timer.start();
        super.onRestart();
    }
}

How do I get the tried questions from my Main Activity into another Activity for a proper review?

3
  • Implement Parcelable interface in QuestionLibrary and put it in intent Commented Dec 5, 2017 at 7:07
  • stackoverflow.com/questions/10071502/… Commented Dec 5, 2017 at 7:08
  • would this help me retrieve the exact questions the user attempted before the timer elapsed? Commented Dec 5, 2017 at 9:43

1 Answer 1

2

You can use below code to send String array via intent

Bundle b=new Bundle();
b.putStringArray(key, stringArrayReference);
Intent i=new Intent(context, Class);
i.putExtras(b);

In order to get the String array in other Activity you can use below code

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);

Hope that helps

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for this. but how do i get the stringArrayReference? parcelable interface?
As in your code I can see string array having reference name as mQuestions , use that reference or other string array reference
but this would get all the questions from the library. i just want to retrieve the questions the user has attempted before the timer elapsed.
would this help me retrieve the exact questions the user attempted before the timer elapsed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.