14

Is there a way to clear the selected item in a ListView?

The ListView is defined like this:

<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minHeight="50dp"
    android:id="@+id/example_list"
    android:layout_weight="2"
    android:choiceMode="singleChoice"/>

And is filled using a custom Adapter.

The selected item is highlighted using a Selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true" >
    <shape>
      <gradient
       android:startColor="#3E5260"
         android:endColor="#3E5260"
         android:angle="270" />
    </shape>
  </item>
  <item android:state_activated="true">
    <shape>
      <gradient
       android:startColor="#3E5260"
         android:endColor="#3E5260"
         android:angle="270" />
    </shape>
  </item>
</selector>

Now what I really have is 2 ListViews in a single activity and when an item is
selected in one ListView I want to deselect the item in the other ListView.

Both ListViews raise the following handler when an item is clicked:

void DeviceList_Click(object sender, EventArgs e)
{
    //easy enough to check which ListView raised the event
    //but then I need to deselect the selected item in the other listview
}

I've tried things like:

exampleList.SetItemChecked(exampleList.SelectedItemPosition, false);

and

exampleList.SetSelection(-1);

But that does not seem to work.

TimothyP
  • 21,178
  • 26
  • 94
  • 142

5 Answers5

27

Using listView.SetItemChecked(-1, true); works fine here.

Here is my Activity I tested with:

SetContentView(Resource.Layout.Main);
var listView = FindViewById<ListView>(Resource.Id.listView);
_listAdapter = new CustomListAdapter(this);
listView.Adapter = _listAdapter;

var button = FindViewById<Button>(Resource.Id.removeChoice);
button.Click += (sender, args) => listView.SetItemChecked(-1, true);

Main.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
    >
  <ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:choiceMode="singleChoice"
  />
  <Button
    android:id="@+id/removeChoice"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="remove choice"
    />
</LinearLayout>
Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
18

Use clearChoices() to clear the checked state of all items in a ListView

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • 1
    I've tried that as well but does not seem to work (should note I'm using Mono for Android) – TimothyP Feb 26 '13 at 05:12
  • 11
    It seems that you should call adapter.notifyDataSetChanged() after this to make it work. – pvshnik Nov 16 '13 at 10:34
  • After calling `listView.clearChoices()` you may need to call `listView.requestLayout()`. See [this excellent explanation](https://stackoverflow.com/a/48294683/1015595) for more. – Big McLargeHuge Nov 18 '19 at 22:02
2

Its works simple for me:

ListView listView = (ListView) findViewById(R.id.idMyListView);
         listView.clearFocus();
Felipe Marques
  • 131
  • 1
  • 3
1

It's an old question, but just in case someone else needs it in the future, based on @Cheesebaron answer, here's what I did:

On each ListViews' OnItemClickListener set the other's list checked item to false:

list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
            list2.setItemChecked(list2.getSelectedItemPosition(), false);
        }
});

This implementation is in Java, but if you get the logic here you can implement it in C# as well. Hope this helps.

jrsall92
  • 572
  • 1
  • 5
  • 19
0

For CHOICE_MODE_SINGLE / singleChoice

int v = listView.getCheckedItemPosition();
if (v >= 0)
    listView.setItemChecked(v, false);
Mahe
  • 759
  • 1
  • 6
  • 21
  • Not a single technique on this entire page has helped me clear the selection emphasis (a gray background) from my ListView... – Phlip Aug 16 '19 at 19:18
  • I went with the "Dirty Fix" here. Limited use application... https://stackoverflow.com/questions/48253761/how-do-i-clear-listview-selection – Phlip Aug 16 '19 at 19:24