2013-02-26 13 views
11

Seçilen öğeyi ListView'de temizlemenin bir yolu var mı?Clear SingleChoice ListView Selection

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

Ve özel Adaptörü kullanılarak doldurulur:

ListView böyle tanımlanır.

seçilen öğe bir Seçici kullanılarak vurgulanır: Gerçekten ne Şimdi

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

tek bir etkinlikte 2 listviews ve bir öğenin bir ListView seçilen
olduğunda ben öğeyi seçimini istiyorum diğer 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 
} 

gibi şeyler denedim:

Hem listviews bir öğe tıklandığında aşağıdaki işleyici yükseltmek

exampleList.SetItemChecked(exampleList.SelectedItemPosition, false); 

ve

exampleList.SetSelection(-1); 

Ama bu görünmüyor çalışmak.

cevap

25

tüm öğelerin kontrol durumunu temizlemeyi. İşte

Ben ile test benim Hareketi:

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

Bu işe yaramadı, bunu denemedi, teşekkürler – TimothyP

+0

Nedir => notasyonu? Android kodunda kullanıldığını hiç görmedim. –

+0

@IgorGanapolsky Bu bir lambda ifadesidir, anonim bir yöntemdir. Bu C# Java'da 'IOnClickListener' uygulamalarına sahipsiniz. – Cheesebaron

15

Kullanım clearChoices()listView.SetItemChecked(-1, true); burada çalışıyor kullanarak bir ListView

+1

Bunu yanı denedim ama (Android için Mono kullanıyorum belirtmeliyiz) çalışmaya – TimothyP

+10

Size o iş yapmak için bundan sonra adapter.notifyDataSetChanged() çağırmalıdır görünüyor görünmüyor. – pvshnik

+0

Bu, bir ListFragment için çalışmaz. –

2

Onun benim için basit çalışır:

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

Neden clearFocus() tek seçenekli bir ListView'i temizler? Anlamı yok. –

2

Bu eski bir soru, ama sadece Gelecekte bir başkasının buna ihtiyacı varsa, @Cheesebaron cevabına göre, yaptığım şey şu:

012 Her listviews' OnItemClickListener set diğerinin liste işaretli öğe false On

:

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

Bu uygulama Java, ancak burada mantık alırsanız siz de C# bunu uygulayabilirsiniz. Bu yardımcı olur umarım.