2013-02-26 29 views
11

क्या सूची दृश्य में चयनित आइटम को साफ़ करने का कोई तरीका है?साफ़ सिंगलचॉइस सूची दृश्य चयन

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 listviews है और मैं में आइटम का चयन रद्द करना चाहते हैं एक आइटम
एक 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 
} 

मैं जैसी चीजों की कोशिश की है:

दोनों listviews निम्नलिखित हैंडलर जब एक आइटम क्लिक किया जाता है बढ़ा

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

+0

=> नोटेशन क्या है? मैंने एंड्रॉइड कोड में कभी नहीं देखा है। –

+0

@ इगोरगानापोलस्की यह एक लैम्ब्डा अभिव्यक्ति है, एक अज्ञात विधि है। यह सी # है। जावा में आपके पास 'IOnClickListener' कार्यान्वयन हैं। – Cheesebaron

15

उपयोग clearChoices() एक ListView

+1

मैंने कोशिश की है लेकिन यह काम नहीं कर रहा है (ध्यान दें कि मैं एंड्रॉइड के लिए मोनो का उपयोग कर रहा हूं) – TimothyP

+10

ऐसा लगता है कि आपको इसे काम करने के लिए adapter.notifyDataSetChanged() को कॉल करना चाहिए। – pvshnik

+0

यह एक सूचीफ्रेजमेंट के लिए काम नहीं करता है। –

2

इसका मेरे लिए आसान काम करता है:

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

स्पष्ट फोकस() एक-पसंद सूची दृश्य को साफ़ क्यों करेगा? कुछ समझ नहीं आया। –

2

यह एक पुराने सवाल है, लेकिन सिर्फ यदि किसी और को भविष्य में इसकी आवश्यकता होती है, तो @ चेसबरन उत्तर के आधार पर, मैंने यह किया है:

प्रत्येक 'listviews OnItemClickListener गलत पर सेट के अन्य सूची की जाँच की मद में:

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

इस कार्यान्वयन जावा में है, लेकिन अगर आप तर्क यहाँ प्राप्त कर आप के रूप में अच्छी सी # में इसे लागू कर सकते हैं। उम्मीद है की यह मदद करेगा।