Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Get sum of EditText values in a ListView on button click
    primarykey
    data
    text
    <p>I have a ListView that has an EditText in each row. The user can update the value that is populated in the EditText, and then click a button to refresh the new sum at the top of the ListView. My approach here is to loop through all ListView records and obtain the value of the EditText, perform the necessary calculations, then update the UI. My problem is I keep receiving a NullPointerException on my fifth iteration, even though there is definitely a value populated. It works fine for the first four iterations. Is my code correct and/or should I be taking a different approach for what I'm trying to do:</p> <pre><code> button_finish.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { double sumOfAppliedAmounts = 0; Bundle extras = getIntent().getExtras(); double exchange_rate = Double.parseDouble(extras.getString("exchange_rate")) / 100; double convertedPaymentAmount; if (exchange_rate &gt;= 1) convertedPaymentAmount = Double.parseDouble(extras.getString("payment_amount")) / exchange_rate; else convertedPaymentAmount = Double.parseDouble(extras.getString("payment_amount")) * exchange_rate; for (int i = 0; i &lt; (m_listview.getCount()); i++) { v = m_listview.getChildAt(i); applied_amount = (EditText) v.findViewById(R.id.list_text_applied_amount_value); sumOfAppliedAmounts = sumOfAppliedAmounts + Double.parseDouble(applied_amount.getText().toString()); } text_remaining_converted_amount.setText(String.valueOf(convertedPaymentAmount - sumOfAppliedAmounts)); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload