2010-06-24 13 views
5

मेरे पास एक चेकबॉक्स के साथ डेटाबेस से भरा जाने वाला सरल सूची है। मुझे चुने गए सभी चेकबॉक्स में एक हैंडल चाहिए। उस बिंदु पर साफ़ बटन दबाया जाता है, मुझे उन्हें हटाने के लिए सभी चयनित चेक बॉक्स की पंक्ति आईडी की आवश्यकता होती है। ऐसा करने के लिए: मेरे list.xml फ़ाइल इस तरह दिखता है:एंड्रॉइड सूची और चेकबॉक्स

< 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/android:list" 
     android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      /> 

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/clearselected" 
     android:text="CLEAR" 
     android:clickable ="false"/> 

</LinearLayout> 

और मेरे data_entry.xml इस तरह दिखता है:

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
      <CheckBox 
      android:id="@+id/CheckBox" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_x="50px" 
      android:layout_y="22px"></CheckBox> 
<TextView android:text="@+id/EntryText" 
      android:id="@+id/EntryText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="@color/blue"/> 

</LinearLayout> 

अब: मैं list.java फ़ाइल जहाँ मैं पॉप्युलेट रहा है सूची इस प्रकार है:

private void populateList() { 
    Cursor c = db1.getAllList(); 
     String[] fields = new String[]{db1.get_data()}; 

     SimpleCursorAdapter cursorAdapter = new 

    ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.data_entry, c, 
      fields, new int[] {R.id.EntryText}); 


    setListAdapter(adapter); 
} 

अब मैं कहाँ heckbox कारण करने के लिए संभाल देते हैं कहीं और यह मुझे एक अशक्त अपवाद के रूप में देना होगा data_entry चेक बॉक्स शामिल हैं। इसके अलावा मुझे चेकबॉक्स स्थिति को संभालने के लिए श्रोता की आवश्यकता है? मैं इस बिंदु पर बस कोई सुराग नहीं लगा हूं ..

उत्तर

19

अपने आप को घुमाने के बजाय, CHOICE_MODE_MULTIPLE सक्षम के साथ मानक सूची दृश्य का उपयोग क्यों न करें?

listView.setChoiceMode(CHOICE_MODE_MULTIPLE); 
listView.setAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_multiple_choice, fields)); 

फिर आप चेक किए गए आइटमों के लिए सूची देखें।

listView.getCheckedItemPositions(); 
+0

धन्यवाद, यही वह है जिसे मैंने नहीं सोचा था। खैर मेरे पास अब एक और सवाल है। मेरे मामले में मेरे पास कर्सर नीचे है: निजी स्थिर स्ट्रिंग [] फ़ील्ड; कर्सर सी = डीबी 1.getAllList(); setListAdapter (नया ArrayAdapter (यह, android.R.layout.simple_list_item_multiple_choice, फ़ील्ड)); अंतिम सूची दृश्य सूची देखें = getListView(); \t listView.setItemsCanFocus (झूठा); listView.setChoiceMode (ListView.CHOICE_MODE_MULTIPLE); मैं कर्सर से फ़ील्ड सरणी में मान कैसे प्राप्त करूं ?? सरल कर्सर एडाप्टर का उपयोग करते समय मैं वहां cusror गुजर रहा था? धन्यवाद प्रेरणा – Prerna

+0

धन्यवाद! यह वास्तव में मुझे बहुत मदद की है। मेरी भी यही समस्या थी। लेकिन अब मुझे पता है कि यह कैसे करें। –

संबंधित मुद्दे