2011-10-10 16 views
22

मेरे पास प्रत्येक पंक्ति में एक सूची और एक चेकबॉक्स है। मैं चाहता हूं कि जब भी मैं एक पंक्ति पर क्लिक करता हूं, तो चेकबॉक्स तदनुसार इसके राज्य को बदल देता है।सूची आइटम पर चेकबॉक्स स्थिति बदलें क्लिक करें?

checkbox.xml मेरी सूची के लिए

<CheckBox 
    android:id="@+id/CheckBox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:focusable="false" 
    android:text="" /> 

क्लिक श्रोता

protected void onListItemClick(ListView l, View v, int position, long id) { 
    Toast.makeText(getApplicationContext(), "You have selected item no." 
      + (position + 1) + "", Toast.LENGTH_SHORT).show(); 

    super.onListItemClick(l, v, position, id); 
    selectedRow = position; 

    if (v == null) { 
     LayoutInflater lif = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = lif.inflate(R.layout.posting_detail_question_row, null); 
    } 

    if (checkedTextView.isChecked()) { 
     Toast.makeText(getApplicationContext(), "Already voted", 
       Toast.LENGTH_SHORT).show(); 
    } else { 
     String[] from = {"option", "votes"}; // values to fetch from 
     // hashmap 
     int[] to = {R.id.option, R.id.votes}; // id's for the view 

     SimpleAdapter adp = new SimpleAdapter(getApplicationContext(), 
       hashList, R.layout.posting_detail_question_row, from, to); 
     setListAdapter(adp); 
     checkedTextView.setChecked(true); 
    } 
} 

posting_detail_question_row.xml

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="40dp" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:background="@drawable/poll_border_top_bg" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/option" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="10dp" 
      android:layout_weight=".3" 
      android:gravity="center_vertical" 
      android:text="Answer 1" 
      android:textColor="#333333" 
      android:textSize="15sp" /> 

     <TextView 
      android:id="@+id/votes" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="10dp" 
      android:layout_weight="1" 
      android:gravity="center_vertical" 
      android:textColor="#333333" 
      android:textSize="15sp" /> 

     <CheckBox 
      android:id="@+id/CheckBox" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:focusable="false" 
      android:text="" /> 
    </LinearLayout> 
</LinearLayout> 

मेरे अनुकूलक एक SimpleAdapter है:

SimpleAdapter adp = new SimpleAdapter(getApplicationContext(), 
      hashList, R.layout.posting_detail_question_row, from, to); 
    Log.v("MyLog", "after simple adapter"); 
    setListAdapter(adp); 
+1

पोस्ट यहाँ posting_detail_question_row.xml लेआउट कोड –

+0

चेकआउट इस http://www.vogella.de/articles/AndroidListView/ article.html # overview_listview –

+0

@ पी एम-परेश मायानी अब इसे जांचें सर –

उत्तर

39

आप findViewById उपयोग करने के लिए अपने चेकबॉक्स के लिए सूचक प्राप्त करने की आवश्यकता शून्य हो लेकिन मैंने उचित जांच की है।

+0

@ मर्लिन यह महोदय काम नहीं कर रहा है, यह रनटाइम त्रुटि दिखा रहा है ... मैं फिर से अपना प्रश्न दोबारा दोहरा दूंगा..मैं चाहता हूं कि जब मैं किसी भी आर पर क्लिक करूं सूची के ओएस के अनुसार चेकबॉक्स को –

+0

के अनुसार चेक किया जाना चाहिए, मैंने गलती की गलती देखी थी, यह 'आर.आईडी.चेकबॉक्स' होना चाहिए था, लेकिन मुझे यकीन है कि आपने इसे देखा है ... क्या आप लॉगकैट देख सकते हैं और देख सकते हैं कि त्रुटि – Merlin

+0

@ मर्लिन आपके जवाब ने मुझे एक गड़बड़ विचार दिया है कि मैं क्या खो रहा हूं यानी (v! = null) भाग लेकिन फिर भी मैं अटक गया हूं, अगर मैं आपके अनुसार करता हूं तो मेरी समस्या हल हो जाती है लेकिन जब मैं दो बार क्लिक करता हूं तो यह काम करता है क्योंकि मेरा एडाप्टर बनाया गया है अन्यथा खंड और इसमें चेकबॉक्स गुम है ... मैंने कोड को अपडेट किया है, क्या आप इसे देख सकते हैं और मुझे कुछ सलाह दे सकते हैं .. हालांकि उत्तर के लिए धन्यवाद .. –

2
CheckBox checkBox = (CheckBox) v.findViewById(R.id.CheckBox); 
checkBox.setChecked(true); 
+0

काम नहीं कर रहे सर –

+0

आपको बहुत धन्यवाद: डी –

7

onListItemClick अंदर ऐसा करते हैं:

protected void onListItemClick(ListView l, View v, int position, long id) { 

    Toast.makeText(getApplicationContext(), "You have selected item no." 
       +(position+1)+"", Toast.LENGTH_SHORT).show(); 

    super.onListItemClick(l, v, position, id); 

    if (v != null) { 
     CheckBox checkBox = (CheckBox)v.findViewById(R.id.Checkbox); 
     checkBox.setChecked(!checkBox.isChecked()); 
    } 
} 

आप onClick ईवेंट तो प्राप्त कर रहे हैं v चाहिए कभी नहीं:

CheckBox cb = (CheckBox) v.findViewById(R.id.CheckBox); 
cb.setChecked(!cd.isChecked()); 
0

यहाँ, पूर्ण कोड है आप addapter सेट करने के बाद इस जोड़ सकते हैं:

list.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 


     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

      String message = "Item " + position; 
      Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show(); 

      CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox); 
      cb.setChecked(!cb.isChecked()); 


     } 


    }); 
संबंधित मुद्दे