1
votes

I've created listview with dynamically items inside which contains two items: CheckBox and TextView. When user taps checkbox inside item it updates background color of textView. It works fine until this item is scrolled out of the visible for user space of list and I get error: java.lang.NullPointerException at com.viewactivities.AddNewPosition.changeTextColorWhenCheckBoxIsClicked(AddNewPosition.java:183).

I have also implemented interface with one method responsible for changing color. I have observed that method "changeTextColorWhenCheckBoxIsClicked" is fired also when item from listView is not visible anymore (Here it causes error). Can anyone help?

Code for class which contains listview and method to change color:

@Override
public void changeTextColorWhenCheckBoxIsClicked(int position) {
    // TODO Auto-generated method stub

    listView.getChildAt(position).findViewById(R.id.listViewItemText).setBackgroundColor(Color.BLUE);

}

And code from listView adapter:

@Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
          ViewHolder viewHolder = null;
          globalPosition = position;

        if(convertView==null){

             LayoutInflater inflater =  ((AddNewPosition) mContext).getLayoutInflater();    
             convertView = inflater.inflate(layoutResourceId, parent, false);

             viewHolder = new ViewHolder();
                viewHolder.text = (TextView) convertView.findViewById(R.id.listViewItemText);
                viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.listViewItemCheckBox);

                viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                        int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                        listOfObjects.get(getPosition).setCheckBoxEnabled(buttonView.isChecked()); // Set the value of checkbox to maintain its state.   changeTextColorWhenCheckBoxIsClicked(getPosition);
                    }
                });
                convertView.setTag(viewHolder);
                convertView.setTag(R.id.listViewItemText, viewHolder.text);
                convertView.setTag(R.id.listViewItemCheckBox, viewHolder.checkbox);
                } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.checkbox.setTag(position); // This line is important.

            viewHolder.text.setText(listOfObjects.get(position).getTextFromListViewItemObject());
            viewHolder.checkbox.setChecked(listOfObjects.get(position).getIsCheckedBoxEnabled());
            //viewHolder.text.setBackgroundColor(Color.RED);

        //ListViewItemObject tmpObject = listOfObjects.get(position);

        //TextView listItemText = (TextView) convertView.findViewById(R.id.listViewItemText);
        //CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.listViewItemCheckBox);
        //checkBox.setChecked(false);

        //listItemText.setText(tmpObject.getTextFromListViewItemObject());

         return convertView;
    }


    @Override
    public void changeTextColorWhenCheckBoxIsClicked(int position) {
        // TODO Auto-generated method stub

        ((AddNewPosition) mContext).changeTextColorWhenCheckBoxIsClicked(position);

    }
1

1 Answers

0
votes

ListView reuses its views so instead of using setTag() use a list to maintain the position.

viewHolder.checkbox.setTag(position); // This line is important.

Something like this - Android Listview Scrolling challenge for row elements