Nullpointerexception on getChildAt

2019-08-27 07:02发布

问题:

I have a listview with some check boxes and i need to check them automatically according to some data that are stored in array. My custom adapter that extends base adapter:

 public class MyAdapter extends BaseAdapter 
    {
        private Context context;

        public SPCMjereAdapter(Context c) 
        {           
            context = c;                    
        }

        public int getCount() {
            return MyArrList.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public int getlistItemsCount()
        {
            return listView.getChildCount();
        }

        public View getView(final int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_list_row, null);

            }   
            // ColID
            TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
            txtOpis.setText(MyArrList.get(position).get("OpisMjere") +".");

            // ColCode
            TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
            txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));


            // ColChk               
            CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
            Chk.setTag(MyArrList.get(position).get("RbMjere"));


            return convertView;

        }

    }

And this is how i check the items

int k=0;
    int j=0;
    for (j=0; j<numberOfItems; j++)
    {

        LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j); // Find by under LinearLayout
        CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk);

        for (k=0; k<rbmjere.size(); k++)
        {
            if (checkbox.getTag().toString() == rbmjere.get(k).toString())
            {
                checkbox.setChecked(true);
            }
        }   
    }

The problem is at line

LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j);

So if I call this code for checking items the problem is that listview show for example 3 items but the code recognize only 2 items and third item is missing. How to detect when all items are visible or how to detect when rendering of listview is finished?

回答1:

1.Maybe the adapter had not set the listview yet

Handler handler = new Handler();        
    handler.postDelayed(new Runnable() {            
        public void run() {
              //your code
              CheckBox checkbox = (CheckBox)listView.getChildAt(j).findViewById(R.id.ColChk);
              //your code
        }
    }, 500);

2.Maybe you scroll the listview so the index is wrong, reference this Android ListView y position



回答2:

Try to use Array Adapter instead of base adapter or something like this:

public class ArrayListAdapter<T> extends ArrayAdapter<T> {



     private List<T> arrayList;

        public ArrayListAdapter(Context context, int layout, List<T> arrayList) {
            super(context, layout);
            this.arrayList = arrayList;
        }

        @Override
        public T getItem(int position) {
            return arrayList.get(position);
        }

        @Override
        public int getCount() {
            return arrayList.size();
        }
    }

create instance of adapter and set listView.setAdapter(arrayAdapter); I see you don't provide List to adapter its propably why you get nullPointerException



回答3:

Problem is solved. Inside getView() i wrote these few lines and it works just I wanted:

for (k=0; k<rbmjere.size(); k++)
            {
                if (Chk.getTag().toString().equals(rbmjere.get(k).toString()))
                {
                    Chk.setChecked(true);

                }
            }   

Thanks to all on help and ideas.