0

I have a listview which items are CheckedTextViews with checkboxes. However, whenever I check one and scroll the listview and they hide, when I return back to them by rescrolling up they are not checked anymore! I read on internet that listviews, when are scrolled, somehow the hidden items are recycled (returned to their default state if I'm correct) after you scroll back at them. I also read that in the custom adapter, I have to use a method called "setOnCheckedChangeListener", but checkedTextViews do not have that particular method!! So I think my problem might be with listviews. How can I solve this? Thank you.

UPDATE: So this is my custom adapter and model class.

public CustomAdapter(Context c, Model[] resource) {
    super(c, R.layout.list_item, resource);

    this.context = c;
    this.modelItems = resource;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    convertView = inflater.inflate(R.layout.list_item, parent, false);
    cb = (CheckedTextView) convertView.findViewById(R.id.checkBox1);
    pN = (TextView) convertView.findViewById(R.id.numberId);
    pN.setText(modelItems[position].getPhoneNumber());
    cb.setText(modelItems[position].getName());

    return convertView;
}

This is the model class:

  public Model(String name, String phoneNumber, int value) {
    this.name = name;
    this.value = value;
    this.phoneNumber = phoneNumber;
}

public String getName() {
    return this.name;
}

public int getValue() {
    return this.value;
}

public String getPhoneNumber() {
    return this.phoneNumber;
}

2 Answers2

2

You can save the checkBox's state in your adapter using the onCheckedChanged listener. Then when you are creating your viewHolders, set the checked state of the checkbox using the data from your adapter. Hope it helps!

Saulo R.
  • 171
  • 3
  • Then you can implement the onClickListener and there you can get if the view is checked or not. You can use "((CheckedTextView) v).isChecked()" – Saulo R. Apr 27 '16 at 18:30
  • I tried that, but i ran into another problem. When I implement onClickListener, I can not get the position of the checkbox clicked because I have to declare it final in the parameter. If I declare it final it wont change the value anymore... so basically I can not check or uncheck other CheckedTextViews. – user3882221 Apr 27 '16 at 18:33
1

You need to use an object that as the value of the checkBox like

public class FormCheck {

    private String name;
    private boolean isChecked;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setIsChecked(boolean isChecked) {
        this.isChecked = isChecked;
    }

}

and in the getView() you do something like this:

checkedTextView.setChecked(itemArray.get(position).getIsChecked);

    checkedTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkedTextView.isChecked())
                itemArray.get(position).setIsChecked = true;
            else
                itemArray.get(position).setIsChecked = false;
        }
    });
Akariuz
  • 663
  • 5
  • 9
  • CheckedTextView doesn't have onCheckedChangeListener() method, only CheckBoxes have that! – user3882221 Apr 27 '16 at 18:31
  • in your custom view you dont have a CheckBox? – Akariuz Apr 27 '16 at 18:32
  • My custom view uses only one CheckedTextView. Anyway can you explain me why do you create a class FormCheck cause I'm not getting it. – user3882221 Apr 27 '16 at 18:42
  • ListView recycle views when you scroll, so the estate of the view can change, on getView you have to give the view the state you want for the object displayed, and using and object array is a way to save this state. Independent of the scrolling – Akariuz Apr 27 '16 at 18:48
  • http://android.amberfog.com/wp-content/uploads/2010/02/listview_recycler.jpg this image explains it ver well – Akariuz Apr 27 '16 at 18:56
  • I still don't understand how do you manage to fix this problem. I know why it happens but I don't get the "getIsChecked" and what role plays the checkform class. Is there anyway I can contact you in another social network like facebook or just by email please? – user3882221 Apr 27 '16 at 18:57