Tuesday, 1 October 2013

Why dont all my textviews update with 1 button clicks? Requires multiple clicks

Why dont all my textviews update with 1 button clicks? Requires multiple
clicks

I have a simple tablelayout with many textview. When the button is pressed
only a few are updated, but what happens is that, you need to press the
button mutiple times for all the textviews to get updated. Why dont they
all change on the first/only click?
public class Calculator extends Activity {
String[] measurement;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
setTitle("");
getActionBar().setIcon(R.drawable.logo);
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
final Button button = (Button) findViewById(R.id.trapulate);
final EditText value_user_amount =
(EditText)findViewById(R.id.user_amount);
final EditText value_gram_yp =
(EditText)findViewById(R.id.value_gram_yp);
final TextView value_gram_amt =
(TextView)findViewById(R.id.value_gram_amt);
final TextView value_gram_sale =
(TextView)findViewById(R.id.value_gram_sale);
final TextView value_gram_profit =
(TextView)findViewById(R.id.value_gram_profit);
final TextView value_eighth_amt =
(TextView)findViewById(R.id.value_eighth_amt);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.measurements_array, R.layout.spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DecimalFormat numberFormat = new DecimalFormat("#.0");
String spinner_value = spinner.getSelectedItem().toString();
Double total_grams = 56.0;
Double total_price = 400.0;
String user_amount = value_user_amount.getText().toString();
Double user_amount_db = 20.0;
String user_price = value_user_amount.getText().toString();
Double user_price_num = 200.2;
String eighth_amt = value_eighth_amt.getText().toString();
Double eighth_amt_int = Double.parseDouble(eighth_amt);
String gram = value_gram_yp.getText().toString();
Double gram_int = Double.parseDouble(gram);
String gram_yp = value_gram_yp.getText().toString();
Double gram_yp_int = Double.parseDouble(gram_yp);
String gram_amt = value_gram_amt.getText().toString();
Double gram_amt_int = Double.parseDouble(gram_amt);
String gram_sale = value_gram_sale.getText().toString();
Double gram_sale_int = Double.parseDouble(gram_sale);
String gram_profit = value_gram_profit.getText().toString();
Double gram_profit_int = Double.parseDouble(gram_profit);
Double new_gram_amt = user_amount_db / 1;
Double new_gram_sale = gram_amt_int * gram_yp_int;
Double new_gram_profit = gram_sale_int - user_price_num;
if (spinner_value.equals("GRAMS"))
{
eighth_amt_int = user_amount_db / 3.5;
eighth_amt_int = Math.round(eighth_amt_int*1e2)/1e2;
value_eighth_amt.setText(Double.toString(eighth_amt_int));
value_gram_amt.setText(Double.toString(new_gram_amt));
value_gram_sale.setText(Double.toString(new_gram_sale));
value_gram_profit.setText(Double.toString(new_gram_profit));
}else
{
//Toast.makeText(getBaseContext(), "Not equal: "
+spinner_value,Toast.LENGTH_SHORT).show();
}
}
});
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
int index = arg0.getSelectedItemPosition();
// storing string resources into Array
measurement =
getResources().getStringArray(R.array.measurements_array);
//Toast.makeText(getBaseContext(), "You have selected : "
+measurement[index],Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}

No comments:

Post a Comment