I'll try to cut down on the information but this app is supposed to tally a score each time you press a radio button. There are 4 screens with radio buttons and then the fifth screen shows you what your score is.
The calculation always comes to the number 4 though. There is no addition at all no matter what is pressed. Where am I going wrong?
RadioButton gpa1 = (RadioButton) findViewById(R.id.GPA1);RadioButton gpa2 = (RadioButton) findViewById(R.id.GPA2);
RadioButton gpa3 = (RadioButton) findViewById(R.id.GPA3);
RadioButton gpa4 = (RadioButton) findViewById(R.id.GPA4);
//Calculation based on GPA question
if (gpa1.isChecked()){calculation = calculation + 1;}
else{ if (gpa2.isChecked()) {calculation = calculation + 2; }
else{ if (gpa3.isChecked()) {calculation = calculation + 3; }
else{ if (gpa4.isChecked()) {calculation = calculation + 4; }
else{calculation = 0.0; }}}}
This is the first main class and after they select their answer and press the next button it goes to a second page with another question and 4 more answers that are also supposed to be added to the calculation variable.
Here is some more of the coding to show what is fully covered:
@Overridepublic void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
//RESTORES SAVED STATES
if (savedInstanceState == null ){calculation = 0.0;}
else{calculation = savedInstanceState.getDouble(CALCULATION);}
addListenerOnButton();
RadioButton gpa1 = (RadioButton) findViewById(R.id.GPA1);
RadioButton gpa2 = (RadioButton) findViewById(R.id.GPA2);
RadioButton gpa3 = (RadioButton) findViewById(R.id.GPA3);
RadioButton gpa4 = (RadioButton) findViewById(R.id.GPA4);
//Calculation based on GPA question
if (gpa1.isChecked()){calculation = calculation + 1;}
else{ if (gpa2.isChecked()) {calculation = calculation + 2; }
else{ if (gpa3.isChecked()) {calculation = calculation + 3; }
else{ if (gpa4.isChecked()) {calculation = calculation + 4; }
else{calculation = 0.0; }}}}
//Get button to do button stuff like go to the next page
Button butGPA = (Button) findViewById(R.id.butGPA);
butGPA.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent i = new Intent(Main.this, Main2.class);
startActivity(i);
}
});
}
Since your if
statement is in onCreate()
, the value is always going to be the RadioButton
which is checked by default which apparently is the first one and is why you always get 4 for the value. Move
if (gpa1.isChecked()){calculation = calculation + 1;}
else{ if (gpa2.isChecked()) {calculation = calculation + 2; }
else{ if (gpa3.isChecked()) {calculation = calculation + 3; }
else{ if (gpa4.isChecked()) {calculation = calculation + 4; }
else{calculation = 0.0; }}}}
to some event such as the Button
onClick()
or whatever you use to move to the next Activity
. You could also set the value in an onCheckedChangeListener()
.
Remember onCreate()
is only going to be called the first time an Activity
starts after it has been destroyed. So, any code you put in there (unless in some event listener) isn't going to change from within onCreate()
.
Edit
butGPA.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (gpa1.isChecked()){calculation = calculation + 1;}
else{ if (gpa2.isChecked()) {calculation = calculation + 2; }
else{ if (gpa3.isChecked()) {calculation = calculation + 3; }
else{ if (gpa4.isChecked()) {calculation = calculation + 4; }
else{calculation = 0.0; }}}}
Intent i = new Intent(Main.this, Main2.class);
i.putExtra("calc", calculation);
startActivity(i);
}
});
Pass the value to each Activity
like that then to get the value (in or after onCreate()
) do something like
Intent i = getIntent();
double calc = i.getDoubleExtra("calc", 0.0);
That is assuming the variable is originally a double
though I'm not sure why it isn't an int
. You would want to check for null
and things but that should get you started anyway.
you can use
newIntent.putExtra("varname",value);
to set the attribute value. and can get the value at new intent
getStringExtra("varname");
and pls refer this question: How do you pass data/parameters to another activity in Android. it may help.