Is there a way to clear the selected item in a ListView?
The ListView is defined like this:
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>
Use clearChoices() to clear the checked state of all items in a ListView
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.
Its works simple for me:
ListView listView = (ListView) findViewById(R.id.idMyListView);
listView.clearFocus();
For CHOICE_MODE_SINGLE / singleChoice
int v = listView.getCheckedItemPosition();
if (v >= 0)
listView.setItemChecked(v, false);