
I’m taking Questions and Answers in String[].The TextView has questions and RadioButton has corresponding answers. If Start Button is clicked, first set of question and answers is displayed in Linear Layout1. If Next Button is clicked LinearLayout1 is replaced by next set of question with answers and again if Next Buttonis clicked next set of question has to be replaced and vice versa. Help me in writing code for Next Button
Note: Only LinearLayout 1 should be changed when clicking Next Button
code:
TextView tv;
RadioButton rb1, rb2;
Button bStart, bNext;
String question[] = { "Key Lime Pie is upcoming version",
"Android is Middleware", "Which is latest Android version?" };
String answerA[] = { "True", "False" };
String answerB[] = { "True", "False" };
String answerC[] = { "Android 4.2", "Android 5" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ques);
tv = (TextView) findViewById(R.id.textView111);
rb1 = (RadioButton) findViewById(R.id.radioButton1);
rb2 = (RadioButton) findViewById(R.id.radioButton2);
bStart = (Button) findViewById(R.id.startExam);
bNext = (Button) findViewById(R.id.bNext);
bStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText(question[0]);
rb1.setText(answerA[0]);
rb2.setText(answerA[1]);
}
});
bNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText(question[1]);
rb1.setText(answerB[0]);
rb2.setText(answerB[1]);
}
});
}
This is a logic problem.
question.rb1andrb2.By using what you have there you will have problems extending that code. After all, are you really going to create a new variable for each different set of answers? It does not make any sense. A multidimensional array works.
However… although you can improve your arrays structure and use an
intto keep track of the current displayed question (and thus increment it to get the next question after pressing thebNext), I suggest you try to create more meaningful objects, like aQuestionclass, with relevant members (title,optionA,optionB, whatever…), especially because it looks like you’re using this code to learn how to program a quiz-styled application.Then you can create various questions and iterate through then.
But is that the issue there? No. The problem is keeping track of questions and answers. Use a multidimensional array with a tracking variable.
// edited: OK, I’m going to suggest something very quick for you:
As you can, both buttons do the same thing, so it’s probably better to only use a next button.