Reputation: 101
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?
Upvotes: 0
Views: 84
Reputation: 4678
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
Upvotes: 2