Question

I have a listview with SimpleCursorAdapter in a ListFragment. The item is CheckedTextView. It has been worked fine. Now I tried to put the ListFragment into a pageview with FragmentPagerAdapter, so that this fragment can be one of the tabs that can respond to gesture.
However, it behaves weirdly. When I click once to check an item, and then scroll down the page (the list is long), then there are several items checked, not just one. I wonder why it behaves like this. Another thing I noticed is that if I don't add

((CheckedTextView)v).toggle();

in the onListItemClick method, then clicking does not set the checkmark on an item. I did not have to do this without the pageview. Could you help me to figure out why the behavior is changed when I put this ListFragment into a Pageview?

        public void onListItemClick(ListView lv, View v, int position, long id) {


        ((CheckedTextView)v).toggle();
         lv.getCheckedItemCount();
         ...
         }

}
Was it helpful?

Solution

It sounds like you're just using SimpleCursorAdapter as-is and not subclassing it to override bindView(). Since the list view recycles views, and thus CheckedTextView, it might be recycling an existing CheckedTextView which might already be set.

Try subclassing and overriding bindView() (or if you're already overriding it) to set the checked state. Something like this:

public class MyAdapter extends SimpleCursorAdapter {

  @Override
  public void bindView( View v, Context context, Cursor c) {
     super.bindView(v,context,c);

     CheckedTextView ctv = (CheckedTextView)findViewById(R.id.check_text_view); // or whatever the id is
     ctv.setChecked( c.getInt(COLUMN_NUMBER) == 1 );

  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top