Android ListView Remove Selection
Asked Answered
T

5

8

Is there a way to elegantly remove the selection on a ListView. It would act in the opposite manner of ListView#setSelection(int position). I would like to programmatically remove the selection from the ListView (such that no item is selected). It does not appear the API has an easy way to do it. Any thoughts?

My question is similar to: Android: disable ListView selection

Thaothapa answered 9/11, 2011 at 19:51 Comment(0)
M
3

None of the other answers worked for me so I did this:

    SparseBooleanArray positions = getListView().getCheckedItemPositions();
    for (int i = 0; i < positions.size(); i++) {
        getListView().setItemChecked(positions.keyAt(i), false);
    }

Not very elegant but it works.

Meaty answered 28/12, 2015 at 0:51 Comment(0)
T
0

Ok what i could do for this set the selection color and background both of the same color if possible or set selection with full alpha value that makes it transparent and even if selection is there it wont be visible anymore

Thunderclap answered 1/12, 2011 at 9:15 Comment(0)
D
0

The simpliest way I could find is to refresh the adapter the ListView is using:

listAdapter.notifyDataSetChanged();

ListView will then refresh, having the same choices but the selection highlight will be cleared.

Draco answered 7/3, 2013 at 12:42 Comment(3)
This method does not exists (in my api v.19)Ligneous
@Asterius, It's a method of the list adapter, not the ListView itself.Athalia
I would also like to comment that this is a method of the BaseAdapter concrete class, not the ListAdapter interface. A cast may be required if the type of the variable listAdapter is ListAdapter.Comma
L
0

Use listView.setItemChecked(-1, true);

Ligneous answered 8/4, 2015 at 9:41 Comment(0)
O
0

I use this method to clear a ListView selection:
listView.SelectedItem = null;

Overman answered 5/6, 2019 at 1:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.