I am trying to make a quiz where users choose an answer in each activity, and the final page provides an answer based on the chosen options. Therefore, I want to pass these values to the final activity, but only the last chosen value seems to show.
First Activity:
public class Quiz extends Activity {
Button btn;
RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz);
btn = (Button) findViewById(R.id.nextBtn);
rg= (RadioGroup) findViewById(R.id.rg);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (rg.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent = new Intent(getApplicationContext(), Quiz1.class);
Bundle bundle = new Bundle();
int id = rg.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
Second Activity:
public class Quiz1 extends Activity {
Button btn;
RadioGroup rg1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz1);
btn = (Button) findViewById(R.id.nextBtn1);
rg1= (RadioGroup) findViewById(R.id.rg1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (rg1.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent1 = new Intent(getApplicationContext(), Quiz2.class);
Bundle bundle1 = new Bundle();
int id = rg1.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle1.putString("rg1", radioButton.getText().toString());
intent1.putExtras(bundle1);
startActivity(intent1);
}
}
});
}
}
Now this follows for a total of 7 activities.. not including the final activity Here is the 7ths (called Quiz6) activity:
public class Quiz6 extends Activity {
Button btn;
RadioGroup rg6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz6);
btn = (Button) findViewById(R.id.nextBtn6);
rg6= (RadioGroup) findViewById(R.id.rg6);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (rg6.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent6 = new Intent(getApplicationContext(), Final1.class);
Bundle bundle6 = new Bundle();
int id = rg6.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle6.putString("rg6", radioButton.getText().toString());
intent6.putExtras(bundle6);
startActivity(intent6);
}
}
});
}
}
You get the idea :)
Here is the FINAL activity (called Final1) here the results are show
here is the code
public class Final1 extends Activity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final1);
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
Bundle bundle1 = getIntent().getExtras();
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView.setText(bundle1.getCharSequence("rg1"));
Bundle bundle2 = getIntent().getExtras();
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView.setText(bundle2.getCharSequence("rg2"));
Bundle bundle3 = getIntent().getExtras();
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView.setText(bundle3.getCharSequence("rg3"));
Bundle bundle4 = getIntent().getExtras();
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView.setText(bundle4.getCharSequence("rg4"));
Bundle bundle5 = getIntent().getExtras();
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView.setText(bundle5.getCharSequence("rg5"));
Bundle bundle6 = getIntent().getExtras();
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView.setText(bundle6.getCharSequence("rg6"));
btn = (Button)findViewById(R.id.restartBtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(v.getContext(), Quiz.class);
startActivityForResult(in, 0);
}
});
}
}
What ends up happening once I run the program is that only "textView" ends up changing to the chosen choice on the activity right before the final activity, shown above
Any help is appreciated, thanks lots <3