Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear SingleChoice ListView selection

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.

like image 685
TimothyP Avatar asked Feb 26 '13 04:02

TimothyP


People also ask

How to clear Selected item in ListView c#?

For a single selection ListView set SelectedItem to null. For a multiple or extended selection ListView you can also call Clear() on the SelectedItems collection.

What is a ListView?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.

What is the use of ListView explain list view with example?

Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.

Why do we use ListView?

Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.


1 Answers

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>
like image 181
Cheesebaron Avatar answered Sep 19 '22 18:09

Cheesebaron