简体   繁体   中英

Android - Get sum of EditText values in a ListView on button click

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:

    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 >= 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 < (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));
        }
    });
Listview lv = (ListView) findViewById(R.id.previewlist);

    final BaseAdapter adapter = new PreviewAdapter(this, name, age);

    confirm.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            View view = null;

            String value;
            for (int i = 0; i < adapter.getCount(); i++) {

                view = adapter.getView(i, view, lv);

                Textview et = (TextView) view.findViewById(R.id.passfare);


                value=et.getText().toString();

                 Toast.makeText(getApplicationContext(), value,
                 Toast.LENGTH_SHORT).show();
            }



        }
    });

I am not sure if this is the problem or if you are aware of the ListView procedures but the getChild() method returns values that are relative to the views position as it is shown on the screen, not the entire position relative to the entire Adapter. I suspect that your screen is displaying less views than there are total and this is why you are getting a null pointer.

This post may be the same question as yours:

android ListView mListView.getChildAt(i) is null, how to solve it?

hope this helps.

Try below code : -

View v1=m_listview.getChildAt(m_listview.getPositionForView(v)-m_listview.getFirstVisiblePosition());

where v is View of onClick() .

Hope It can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM