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
An activity's intent only has one Bundle associated with it. Thus, you are only passing the last bundle to your final activity, the others are passed to the immediately following activity instead. You can fix this issue by doing
Bundle bundle = getIntent().getExtras(); //this gets the current activity's extras
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg1", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
instead of creating a new bundle each time as you do now. then pass this bundle along to the following activity. That way, each time you get a new answer, you are adding it to your bundle of answers, rather than creating a new bundle with only one item each time.
Then in your final activity, you only have to get the one bundle, and can pull all the answers out of it. NOTE: You'll want to make sure that your'e updating the correct textview with the correct text. In your question, textView is the only view that gets updated, 6 times, and the others do not get updated at all.
Bundle bundle = getIntent().getExtras();
TextView textView = (TextView)findViewById(R.id.txt);
textView.setText(bundle.getCharSequence("rg"));
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));
Related
I have have a problem here with my code. I want to open the next Activity using submit button but I'm having issues. Can anybody help me on the mistake I am making so that I can implement it? Thanks
public class Chairperson extends Activity implements View.OnClickListener{
TextView textView;
Button submit_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chairperson);
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(this);
textView = (TextView) findViewById(R.id.welcome_txt);
String message = getIntent().getStringExtra("message");
textView.setText(message);
Button submit_btn = (Button) findViewById(R.id.submit_btn);
final TextView submitTextView = (TextView) findViewById(R.id.submitTextView);
final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
submit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Grou[
int selectedRadioButtonID = rg1.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if
(selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
submitTextView.setText(selectedRadioButtonText + " selected.");
} else {
submitTextView.setText("Nothing selected .");
}
}
});
}
#Override
public void onClick(View v) {
startActivity(new Intent(this, ViceChairperson.class));
}
}
I have written a code for your button, delete all previous code for submit_btn in your code and replace with this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (radioGroup.getCheckedRadioButtonId() == -1)
{
Toast.makeText(context, "Select an option.", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(context, ViceChairperson.class);
startActivity(intent);
finish();
}
}
});
}
}
If you have any issues please let me know.
Just move the line
startActivity(new Intent(getApplicationContext(), ViceChairperson.class));
after the if (selectedRadioButtonID != -1) check. If that check succeeds you start the new activity, if not, nothing is launched.
There's no need for the second onClick method, which is not bound to anything and will never be invoked.
My question is relative to this question Clearing intent
but I'm having problems implementing it.
My first class TodaysExercise.java has a button, when the button is clicked I putExtra intent.putExtra("highlegs", "High Legs"); and startActivity(intent);
TodaysExercise.java
final Intent intent = getIntent();
if (intent != null) {
String clicked = intent.getStringExtra("button");
if (clicked.equals("btn1")) {
intent.setClass(TodaysExercise.this, DoExercise.class);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//When btn1 is clicked I putExtra
intent.putExtra("highlegs", "High Legs");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}
The next class DoExercise.java then get the string String text = bundle.getString("highlegs"); and set it to the textView textView.setText(text);. I then check if TextView is equal to if (textView.getText().equals("High Legs")) and if it is, I once again putExtra i.putExtra("next", "Leg Curls X 20"); and start next class startActivity(intent);
DoExercise.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_do_the_exercise);
textView = (TextView) findViewById(R.id.replace_this);
descheader = (TextView) findViewById(R.id.desc_header_id);
fab = (FloatingActionButton) findViewById(R.id.fab);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/countdown.ttf");
descheader.setTypeface(custom_font);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String text = bundle.getString("highlegs");
if (text != null)
textView.setText(text);
}
if (textView.getText().equals("High Legs"))
{
ImageView imLoading = (ImageView) findViewById(R.id.loadingView);
imLoading.setBackgroundResource(R.drawable.workout);
AnimationDrawable frameAnimation = (AnimationDrawable) imLoading
.getBackground();
frameAnimation.start();
final Intent i = new Intent();
i.setClass(DoExercise.this, ReadyForNext.class);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i.putExtra("next", "Leg Curls X 20");
startActivity(i);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
}
});
}
I get String text = bundle.getString("next"); and set tv_nextdesc.setText(text); the string extras to the TextView. I also have a button in this class that should return to DoExercise.java and this is where my question is.
ReadyForNext.java
public class ReadyForNext extends AppCompatActivity {
Button btn_next_exercise;
TextView tv_nextdesc;
TextView tv_nexttxt;
Context f;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ready_for_next);
btn_next_exercise = (Button) findViewById(R.id.btn_next_exercise);
tv_nextdesc = (TextView) findViewById(R.id.tv_nextdesc);
tv_nexttxt = (TextView) findViewById(R.id.nxtTxt);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/countdown.ttf");
tv_nextdesc.setTypeface(custom_font);
tv_nexttxt.setTypeface(custom_font);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String text = bundle.getString("next");
if (text != null)
tv_nextdesc.setText(text);
}
final Intent i = new Intent();
i.setClass(ReadyForNext.this, DoExercise.class);
btn_next_exercise.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//I first check what the title is to determine what to put extra
if (tv_nextdesc.getText().equals("Leg Curls X 20")) {
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
//then here I putExtra
i.putExtra("legcurls", "Leg Curls");
startActivity(i);
}
}
});
}
So as you can see I have 3 classes that basically run in a circle, TodaysExercise.java opens DoExercise.java and it opens ReadyForNext.java from here I want to open DoExercise.java again (Reuse DoExercise.java) but that is when I want to clear the intent to put a new intent extra?
Any help on how to achieve this?
Just finish your DoExercise activity here
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
i.putExtra("next", "Leg Curls X 20");
startActivity(i);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
}
});
and start from ReadyForNext activity like you're doing.
Now you have to do some changes in DoExercise class because your intent putExtra key change with different class otherwise you'll get null pointerException
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if(bundle.containsKey("highlegs")){
String text = bundle.getString("highlegs");
}
if(bundle.containsKey("legcurls")){
String text = bundle.getString("legcurls");
}
if (text != null)
textView.setText(text);
}
Hope this will help you.
I'm trying to send a variable from one activity to another. When I try to test the app on my phone, it crashes and says the app has stopped working.
Code from sending side
if (id==R.id.action_cart){
Intent good1 = new Intent(MainActivity.this, Scroll.class);
good1.putExtra("intVariableName", 5);
startActivity(good1);
TextView hides = (TextView) findViewById(R.id.textView3);
hides.setVisibility(View.INVISIBLE);
}
and code on receiving side
public class Scroll extends AppCompatActivity {
TextView btn = (TextView) findViewById(R.id.text);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(intValue);
}}
If you know what the issue is please let me know. I've spent hours looking for a solution. Thank you
you need to move this TextView btn = (TextView) findViewById(R.id.text); inside oncreate because before oncreate there is no layout attached to Scroll activity and finding textview by using this findViewById(R.id.text) will result in a failure hence crash
public class Scroll extends AppCompatActivity {
TextView btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
btn = (TextView) findViewById(R.id.text)
//^^^^
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(""+intValue);
}
}
Note : Use appropriate naming convention for clarity , like textViewVar for TextView instead of btn and convert your int value to text using ""+value because TextView allow only String to use
I think the problem is in this call: btn.setText(intValue);: string resource ID with value 5 does not exist. Try replacing it with btn.setText(Integer.toString(intValue));
Try this
if (id==R.id.action_cart){
//hide views before moving
TextView hides = (TextView) findViewById(R.id.textView3);
hides.setVisibility(View.INVISIBLE);
Intent good1 = new Intent(MainActivity.this, Scroll.class);
good1.putExtra("intVariableName", 5);
startActivity(good1);
}
and in Recieving activity
put this in oncreate
btn = (TextView) findViewById(R.id.text);
Like this
public class Scroll extends AppCompatActivity {
TextView btn ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
btn = (TextView) findViewById(R.id.text);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(intValue);
}}
change a line to: btn.setText(String.valueOf(intValue));
public class Scroll extends AppCompatActivity {
TextView btn = (TextView) findViewById(R.id.text);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(String.valueOf(intValue));
}}
In your activity
public class Scroll extends AppCompatActivity {
TextView btn ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
btn = (TextView) findViewById(R.id.text); // initialize your button here
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(String.valueOf(intValue));
}}
Replace your code by this
public class Scroll extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
TextView btn = (TextView) findViewById(R.id.text);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
btn.setText(intValue);
}
}
so a quick question. In my app, the users go through multiple activities that provides them with radio-buttons to choose from.. at the final activity, based on there options, the will be shown which character they are etc... Now the problem is I don't know how to write a code in order to do that. Here is what I have
First 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 intent = new Intent(getApplicationContext(), Quiz2.class);
Bundle bundle = getIntent().getExtras();
int id = rg1.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg1", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
Second activity
Button btn;
RadioGroup rg2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz2);
btn = (Button) findViewById(R.id.nextBtn2);
rg2= (RadioGroup) findViewById(R.id.rg2);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rg2.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Please select an answer",
Toast.LENGTH_SHORT).show();
} else{
Intent intent = new Intent(getApplicationContext(), Quiz3.class);
Bundle bundle = getIntent().getExtras();
int id = rg2.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
bundle.putString("rg2", radioButton.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
This continues for about 7 activities
Final activity (where the result and the character are shown)
public class Final1 extends Activity {
Button btnRestart;
Button btnShare;
#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"));
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));
btnRestart = (Button)findViewById(R.id.restartBtn);
btnRestart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(v.getContext(), Quiz.class);
startActivityForResult(in, 0);
}
});
btnShare = (Button)findViewById(R.id.btnShare);
btnShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "check out this app";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
}
}
Now, in the final activity, I want to have a code where if for example:
if in activity1(Quiz1) options 1 OR 2 are chosen ( as in the radio-buttons selected),
Quiz2: options 2 or 4
Quiz3: options 1 or 4
Quiz2: options 2 or 3
and so on...
then change a textview to something specific like "your character is x"
I have already carried all the information to the final class, I just don't know how to approach this problem, even-though it sounds simple.
Any help would be appreciated, thank you <3
EDIT:
TextView textviewResult = (TextView) findViewById(R.id.textViewResult);
if(bundle.getString("rg").equals("A")||(bundle.getString("rg").equals("B")&& bundle.getString("rg1").equals("B")&& bundle.getString("rg2").equals("Long range weapons")
&& bundle.getString("rg3").equals("C") || bundle.getString("rg3").equals("D") && bundle.getString("rg4").equals("A")||bundle.getString("rg4").equals("B")
|| bundle.getString("rg4").equals("CC") && bundle.getString("rg5").equals("A") || bundle.getString("rg5").equals("E")
&& bundle.getString("rg6").equals("Yes"))) {
textviewResult.setText("x");
}else{
textviewResult.setText("not x");
}
The problem with this is, even if I choose another option for rg (so not the "A" or "B" options), but then for the rest I choose the ones in the If statement, it still ends up saying x(but it should be saying Not x)
You can use the String equals() method inside your if statement. It returns true if both Strings are equal.
E.g.:
if(bundle.getString("rg").equals("YourRadioButtonText")){
...
}
Your code could look something like this:
`public class Final1 extends Activity {
Button btnRestart;
Button btnShare;
#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"));
TextView textView1 = (TextView)findViewById(R.id.txt1);
textView1.setText(bundle.getCharSequence("rg1"));
TextView textView2 = (TextView)findViewById(R.id.txt2);
textView2.setText(bundle.getCharSequence("rg2"));
TextView textView3 = (TextView)findViewById(R.id.txt3);
textView3.setText(bundle.getCharSequence("rg3"));
TextView textView4 = (TextView)findViewById(R.id.txt4);
textView4.setText(bundle.getCharSequence("rg4"));
TextView textView5 = (TextView)findViewById(R.id.txt5);
textView5.setText(bundle.getCharSequence("rg5"));
TextView textView6 = (TextView)findViewById(R.id.txt6);
textView6.setText(bundle.getCharSequence("rg6"));
// NEW
TextView textviewResult = (TextView) findViewById(R.id.resultTV);
if(bundle.getString("rg").equals("C")){
textviewResult.setText("It is C");
}
else if(bundle.getString("rg1").equals("A") || bundle.getString("rg2").equals("B")){
textviewResult.setText("It is A or B");
}
else if(bundle.getString("rg1").equals("C") && bundle.getString("rg2").equals("D")){
textviewResult.setText("It is C and D");
}
else if(!bundle.getString("rg1").equals("A")){
textviewResult.setText("It is not A");
}
else {
textviewResult.setText("Mhhh");
}
// END
btnRestart = (Button)findViewById(R.id.restartBtn);
btnRestart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(v.getContext(), Quiz.class);
startActivityForResult(in, 0);
}
});
btnShare = (Button)findViewById(R.id.btnShare);
btnShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "check out this app";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
});
}
}`
I started a simple quiz application as below it consists of TextView to show the questions and a RadioGroup consisting of 3 RadioButtons for 3 options. When you finish the quiz it shows number of correct answers and number of wrong answers and your degree. But I have some problems as below Please help me to solve it.
the problems:
When i click on next question its transfer me to result activity not to next questions.
The Result activity page did not show the total result correctly
MainActivity
public class MainActivity extends ActionBarActivity {
TextView tv;
Button btnNext;
RadioGroup rg;
RadioButton rb1,rb2,rb3;
String questions[]={"qqqqq","dddddd"};
String ans[]={"",""};
String opt[]={"","","","","",""};
int flag=0;
public static int marks,correct,wrong;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tvque);
btnNext = (Button) findViewById(R.id.button1);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
rb1 = (RadioButton) findViewById(R.id.radio0);
rb2 = (RadioButton) findViewById(R.id.radio1);
rb3 = (RadioButton) findViewById(R.id.radio2);
tv.setText(questions[flag]);
rb1.setText(opt[0]);
rb2.setText(opt[1]);
rb3.setText(opt[2]);
Toast.makeText(this,"Nigative mark", 1000).show();
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
RadioButton uans=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
String ansText=uans.getText().toString();
if(ansText.equalsIgnoreCase(ans[flag]))
{
correct++;
}
else
{
wrong++;
}
flag++;
if(flag<questions.length)
{
tv.setText(questions[flag]);
rb1.setText(opt[flag*3]);
rb2.setText(opt[(flag*3)+1]);
rb3.setText(opt[(flag*3)+2]);
}
else
{
marks=correct;
}
Intent i=new Intent(getApplicationContext(),ResultActivity.class);
startActivity(i);
}
});
}
}
ResultActivity
public class ResultActivity extends ActionBarActivity {
TextView tv;
Button btRestart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
tv=(TextView) findViewById(R.id.textView1);
btRestart=(Button) findViewById(R.id.button1);
StringBuffer sb=new StringBuffer();
sb.append("Correct ANS:"+MainActivity.correct);
sb.append("Wrong ANS:"+MainActivity.wrong);
sb.append("Final Score:"+MainActivity.marks);
tv.setText(sb);
MainActivity.correct=0;
MainActivity.wrong=0;
btRestart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
}
});
}
}
When i click on next question its transfer me to result activity not to next questions.
You are telling it to go to the ResultsActivity in your Intent
// 2nd param is the target Activity
Intent i=new Intent(getApplicationContext(),ResultActivity.class);
startActivity(i);
To fix that, you would need to use the name of the Activity where your next question is to replace ResultActivity. Unless you store them in an Array or other type of list then you could just replace the question TextView text with the next question.
The Result activity page did not show the total result correctly
This is nearly impossible to answer without knowing your expected and actual results.