1

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));
        }
    });
bgeveritt
  • 303
  • 5
  • 18

3 Answers3

0

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.

Community
  • 1
  • 1
Scott
  • 1,652
  • 1
  • 13
  • 10
  • Thanks - this lead me in the right direction. I wasn't aware that you couldn't loop through ALL items of a ListView (visible or or not.) I've done the process on the adapter layer and it's working well. – bgeveritt Jun 14 '13 at 16:30
  • Glad it was of some help. I find that in certain situations, some of the UI optimizations that Android does can be a headache. – Scott Jun 14 '13 at 21:10
0

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.

duggu
  • 37,851
  • 12
  • 116
  • 113
0
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();
            }



        }
    });
arjun
  • 1
  • 3