Maintaining checkbox states in listview with CursorAdapter
Reason : ListView re-uses the views.
Solution :
class VocabCursorAdapter extends CursorAdapter {
List<Integer> selectedItemsPositions;//to store all selected items position
public VocabCursorAdapter(Context context, Cursor c,int flags) {
super(context, c,0);
selectedItemsPositions = new ArrayList<>();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
View view = LayoutInflater.from(context).inflate(R.layout.item_vocab, viewGroup, false);
CheckBox box = (CheckBox) view.findViewById(R.id.editCheckbox);
box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
int position = (int) compoundButton.getTag();
if (b) {
//check whether its already selected or not
if (!selectedItemsPositions.contains(position))
selectedItemsPositions.add(position);
} else {
//remove position if unchecked checked item
selectedItemsPositions.remove((Object) position);
}
}
});
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
//your other stuff
CheckBox box = (CheckBox) view.findViewById(R.id.editCheckbox);
box.setTag(cursor.getPosition());
if (selectedItemsPositions.contains(cursor.getPosition()))
box.setChecked(true);
else
box.setChecked(false);
}
}
Android: CursorAdapter, ListView and CheckBox
There are a few concerns with the ListView when having checkable items in it. I would suggest the following link:
http://tokudu.com/2010/android-checkable-linear-layout/
I think it's close to what you want.
CursorAdapter in Listview
problem is when you update your database just database going to update not cursor that adapt your cursor adapter ,so you have to use
changeCursor(newcursor);
in your adapter after updating your database.
hope this help you.
Android save Checkbox State in ListView with Cursor Adapter
I would recommend you use Android's built-in support for multiple-choice lists (CHOICE_MODE_MULTIPLE
).
The List11.java
SDK sample demonstrates this. You can also find a project from one of my tutorials that uses it here.
You can still use this technique with your own layout, so long as you include a CheckedTextView
with android:id="@android:id/text1"
as shown in the android.R.layout.simple_list_item_multiple_choice
resource, a copy of which ships with your SDK.
Also, see this question and this question and this question and this question.
Checkbox listener in ListView with CursorAdapter
To avoid triggering callbacks on listener, you should unregister existing one by cb.setOnCheckedChangeListener(null)
then set cb.setChecked(c.getBoolean(c.getColumnIndex("state")))
then cb.setOnCheckedChangeListener()
again.
However you should think of a better way of using that OnCheckedChangeListener
, since you would end up creating listeners more than needed.
Related Topics
Emulator in Android Studio Doesn't Start
How to Set Multiple Alarms Using Alarm Manager in Android
Java.Lang.Outofmemoryerror: Bitmap Size Exceeds Vm Budget
Custom Row in a Listpreference
Launch Activities from Different Package
Get Context of Popupmenu Like Contextmenu
How to Add to My Android Application a Button Than Do Like to a Facebook Page
How to Show Multiple Markers on Mapfragment in Google Map API V2
How to Get Current Flavor in Gradle
Android:Call Activity of Another Application
How Can Make My Viewpager Load Only One Page at a Time Ie Setoffscreenpagelimit(0);
Is There Any Limit of Bundle in Android
Android: Change Shape Color in Runtime
How to Implement a Listview Without Listactivity? (Use Only Activity)
Android Studio 3.1 "Run" Not Compiling Code