Everything works with no errors, but in the xml, tvfinalgrade stays 0.0… Why isn’t the double fin being displayed? I’m sure about what order the tvfin code lines should be written, but I’m assuming its not right.
public class GFActivity extends Activity {
public double fin;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getfinal);
double q1, q2, ex;
EditText etq1, etq2, eteg;
etq1 = (EditText)findViewById(R.id.editText1);
try{
q1 = Double.parseDouble(etq1.getText().toString());
} catch (NumberFormatException e) {
q1=0;
}
etq2 = (EditText)findViewById(R.id.editText2);
try{
q2 = Double.parseDouble(etq2.getText().toString());
} catch (NumberFormatException e){
q2 = 0;
}
eteg = (EditText)findViewById(R.id.editText3);
try{
ex = Double.parseDouble(eteg.getText().toString());
} catch (NumberFormatException e){
ex = 0;
}
fin = 0.4*q1+0.4*q2+0.2*ex;
if(fin == (int)fin){
System.out.println((int)fin);
}
else{
fin = 0.01*((int)(fin*100));
System.out.println(fin);
}
Button solve = (Button)findViewById(R.id.getfinbutton);
solve.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tvfin= TextView)findViewById(R.id.tvfinalgrade);
tvfin.setText(fin+"");
}
});
}
}
When you start the app, in the
onCreate()you already parse theEditTextcontent(which are empty, so you throwNumberFormatExceptionand yourfinvariable ends up as0) and when you get to the part of setting the result(the user clicks theButton) you set thefinwhich is currently0to theTextView(the problem is that when the user clicks theButtonyou never get the currentEditTextcontent to do any calculation and thefinvariable is the old value(0)). Move your calculation in theonClick()method: