2013-02-26 45 views
11

有沒有辦法清除ListView中的選定項目?清除SingleChoice ListView選擇

的ListView的定義是這樣的:

<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"/> 

和使用自定義適配器被填充。

選定的項目使用選擇強調:

<?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> 

現在我真的是在一個單一的活動2名列表視圖,當一個產品在一個ListView中選擇
我要取消選擇項另一個ListView。

兩個列表視圖提高項目被點擊以下處理:

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 
} 

我已經試過了諸如:

exampleList.SetItemChecked(exampleList.SelectedItemPosition, false); 

exampleList.SetSelection(-1); 

但是,這似乎並沒有上班。

回答

25

使用listView.SetItemChecked(-1, true);正常工作在這裏的所有項目的選中狀態。

這是我的活動我測試:

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> 
+0

這似乎工作,沒有試過那個,謝謝 – TimothyP 2013-03-04 01:43:00

+0

什麼是=>符號?我從來沒有見過在android代碼中使用過。 – 2014-06-30 21:53:12

+0

@IgorGanapolsky這是一個lambda表達式,一個匿名方法。這是C#。在Java中,您有'IOnClickListener'實現。 – Cheesebaron 2014-07-01 08:40:54

15

使用clearChoices()以清除在ListView

+1

我試過,但似乎沒有工作(應注意我使用單聲道爲Android) – TimothyP 2013-02-26 05:12:39

+10

看來,你應該調用adapter.notifyDataSetChanged()後,使其工作。 – pvshnik 2013-11-16 10:34:05

+0

這對ListFragment不起作用。 – 2014-06-30 21:55:21

2

它的工作原理簡單對我來說:

ListView listView = (ListView) findViewById(R.id.idMyListView); 
     listView.clearFocus(); 
+5

爲什麼clearFocus()會清除單選ListView?沒有意義。 – 2014-06-30 21:55:01

2

這是一個老問題,但只是以防別人將來需要它時,基於@Cheesebaron的回答,這裏是我所做的:

在每個列表視圖OnItemClickListener設置其他的名單核對項目爲false:

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

這個實現是在Java中,但如果你在這裏得到的邏輯,你可以在C#中實現它。希望這可以幫助。