I have a class for the row, it extends RelativeLayout and inflates some layout to self. A CheckBox checked state listener is also set.
public class LayersListRow extends RelativeLayout {
public LayersListRow (Context context, final GITuple tuple) {
super(context);
LayoutInflater.from(context).inflate(R.layout.layers_list_item, this, true);
...
CheckBox checkbox = (CheckBox)findViewById(R.id.layers_list_item_switch);
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {
...
}
});
}
...
}
I have a custom ArrayAdapter, which is filled with some objects of this class. I want to use them as the view too.
public class LayersAdapter extends ArrayAdapter<LayersListRow> {
@Override
public View getView (int position, View convertView, ViewGroup parent) {
return getItem(position);
}
public LayersAdapter (Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
}
}
But when I set this adapter to my ListView the CheckBox‘s listener doesn’t work. But for the items which appears on scroll down, it works fine.
If I replace return getItem(position); in the getView method with
if (null == convertView)
return getItem(position);
return convertedView;
then for all items visible at first in the list listener works great, but when I try to scroll I get wrong item (the first one again), ListView freezes and don’t respond.
Why is this happening and how should I make it work properly?
When you use this
The view are reused. Hence when you scroll the views that where first shown will again be shown.
I would recommend you to see the answer I have written in this post. If you cannot follow it put a comment down here.
Get checked Listitems from ListView and pass that to another activity