Reputation: 8768
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+"");
}
});
}
}
Upvotes: 0
Views: 85
Reputation: 87064
When you start the app, in the onCreate()
you already parse the EditText
content(which are empty, so you throw NumberFormatException
and your fin
variable ends up as 0
) and when you get to the part of setting the result(the user clicks the Button
) you set the fin
which is currently 0
to the TextView
(the problem is that when the user clicks the Button
you never get the current EditText
content to do any calculation and the fin
variable is the old value(0
)). Move your calculation in the onClick()
method:
Button solve = (Button)findViewById(R.id.getfinbutton);
solve.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
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);
}
TextView tvfin= TextView)findViewById(R.id.tvfinalgrade);
tvfin.setText(fin+"");
}
});
Upvotes: 1