Android list view clarification quest

My scenario:
-I'm using a list view in multiple choice mode to enable a user to delete several items he/she has checked, at once.
-When the user clicks the delete button, i do the following:
-get positions of checked items using: myList.getCheckedItemPositions();
-Get the items in this position and put them to a list - toDeleteList.
-(QUESTION BASED ON THIS STEP) use myList.setItemChecked(position, false) to uncheck the list item.
-Remove the items in the "toDeleteList"

Now, i was "forced" to manually uncheck the list item because the result of myList.getCheckedItemPositions() does not change after deleting from mylist.. i.e

-if, for example, I delete the 1st item (a) of list [a, b, c, d], b will appear checked after the delete ie. in list the list [b, c, d] - after deleting a.

Question is why? Because the SparseBooleanArray returned by myList.getCheckedItemPositions(); is the same before and after deleting from the list - using the adapter.

I thought (i could be wrong) that after removing an item from the list via the adapter, the CheckedItemPositions array should also change to reflect the new state of the list

eg. - mylist = [a, b, c, d]
- then i check items at position 0 and 3 checked (a & d)
- the checked item positions (mylist.getCheckedItemPositions()) array now has values true for positions 0 and 3
- If i remove a & d from the list, therefore, mylist = [b,c], mylist.getCheckedItemPositions() is still the same as above ie. positions 0 and 3 are still checked after deleting items from the list (i think this is not normal - but again i could be wrong)
-I was expecting it not to be checked for positions 0 & 3 because the items that were previously at these positions are no longer in the list.

i'm i getting something wrong here (or having the wrong expectations :) ) ? someone please clarify this..
Thanks in advance,

Asked by: Marcus982 | Posted: 24-01-2022

Answer 1

I was struggling with this same issue and this is what worked for me.

First I've set a cursor adapter that keeps the items checked state based on their ids, since positions aren't useful when the list can be changed dynamically.

private class TagListAdapter extends SimpleCursorAdapter {
     * Stores boolean values for the list items, based on their ids.
    SparseBooleanArray mCheckStates;

    public TagListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        mCheckStates = new SparseBooleanArray();

     * Sets an id as checked/unchecked.
     * @param id
     * @param checked
    public void setChecked(int id, boolean checked) {
        mCheckStates.put(id, checked);

     * @see android.widget.ListAdapter#getView(int, View, ViewGroup)
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        if(!mCursor.moveToPosition(position)) {
            throw new IllegalStateException("couldn't move cursor to position " + position);

        int id = mCursor.getInt(INDEX_OF_COLUMN_ID);
        if(mCheckStates.indexOfKey(id) < 0) {
            // Populate checked value for the first time.
            mCheckStates.put(id, function_that_returns_if_the_item_is_checked(id));

        // Set the item as checked or unchecked based on the value stored in mCheckStates.
        mListView.setItemChecked(position, mCheckStates.get(id, false));
        return view;

Then in my activity I've set a onListItemClick() that executes the related check/uncheck action and sets the checked state in the adapter:

protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    boolean checked = ((CheckedTextView) v).isChecked();

    if(checked == true) {
        // execute uncheck related action
    } else {
        // execute check related action

    // Check/uncheck the item in the adapter.
    ((TagListAdapter) getListAdapter()).setChecked((int) id, !checked);

Answered by: Tess933 | Posted: 25-02-2022

Answer 2

Speaking out my personal opinion, I am assuming the positions would still remain checked because the getCheckedItemPositions() reflects the positions opposed to values, so if positions 0 and 3 are still exist inside the ListView after deletion, they will remain checked.

I could be wrong, just voicing my own opinion. Good luck

Answered by: Kevin302 | Posted: 25-02-2022

Similar questions

Clarification of AlarmManager behavior in Android

I see all the examples of AlarmManager being set by an Activity. My question is this: If my application sets a recurring AlarmManager, does that persist even after the application that started is is closed and removed from memory? If not, how do I start an AlarmManager at a lower level that is started by Android at boot up and if it ever fails or dies or throws an exception is restarted without the user hav...

android - GPS clarification

I'd just like to ask for some clarifications regarding the GPS functionalities of android phones. When an application activates the GPS of a phone, will the phone need to be on a data plan or should activate the data traffic ( Settings > Wireless &amp; Network settings > Mobile Networks > Data Traffic) for the GPS to get the phone's coordinates?

android - Clarification on Intents, Context and overriding default behaviour

To generalize, I understand my application can declare activities to handle specific intents (filtered by category and actions). I can declare my app to handle the ACTION_VIEW and somehow have it magically be registered as a browser to the system. How is this done? Is there a resource that anyone knows of to further read into how which action/category combination lets apps register themselves to the system as, say, browser...

google play - Need clarification on Android Market version requirement in support In-app Billing

I am confused by one statement in Google’s official document: “If your device is running Android 3.0, in-app billing requires version 5.0.12 (or higher) of the MyApps application. If your device is running any other version of Android, in-app billing requires version 2.3.4 (or higher) of the Android Market application.“ I am wondering if it means: Devices running Android 3.0 (Exclude 3....

android - clarification on using toast message

I wanted to use a toast message when searched parameter is invalid. Say, i want to show a toast message as "invalid search" here is my sample code, **** package samples.employeephone; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class...

android - Twitter login clarification

I want to create a twitter application in Android. For this I want to create a login page and login to twitter. For this should we need a Consumer key &amp; Consumer secret Key. What does this mean. To create this login page should we need any this else other than a twitter account. Please provide any code or link to help me.

android - InsertHelper clarification needed

From the doc: What does this line means? but compile the SQL insert statement only once, Please explain, i am not able to understand this. Regards, ...

android - I would love some clarification on the different Activity launch modes

I'm trying to make sense of, but failing to, the documentation's distinction between singleTask and singleInstance. They both look like singletons to me. Could someone please elaborate on how they're different and maybe note an example of where one might choose to use one over the other? I don't need an explanation of the singleTop and the default launchMode, I'm only confused about these two.

Clarification on CursorLoader and LoaderManager android

Good day, I have a class that implements the LoaderCallbacks, and hence have the unimplemented methods overriden. onCreateLoader(),onLoaderFinshed() and onLoaderReset(); in the Activity onCreate(), i prepare a loader: getLoaderManager().initLoader(0, null, this); and in the onCreateLoader(), i have returned a custom loader class here which extends the SimpleLoader class by christai...

Android. ldpi, mdpi, hdpi clarification

So, in regards to this stackoverflow accepted answer: How do I convert ppi into dpi for Android images? So, ok, i make each background image the dimensions that that guy specifies in the answer. BUT, if i define the other ImageViews (smaller ones), that are placed on the screen, in relation to the sizes defin...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android