2011-10-10 31 views
22

Tôi có một danh sách và một hộp kiểm trong mỗi hàng của nó. Tôi muốn rằng bất cứ khi nào tôi nhấp vào một hàng, hộp kiểm sẽ thay đổi trạng thái của nó tương ứng.Thay đổi trạng thái Hộp kiểm trên mục Danh sách nhấp?

checkbox.xml

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

nhấp chuột listener cho danh sách của tôi

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> 

bộ chuyển đổi của tôi là một SimpleAdapter:

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

bài ở đây posting_detail_question_row.xml mã bố trí của bạn –

+0

thanh toán này http://www.vogella.de/articles/AndroidListView/ article.html # overview_listview –

+0

@ P M- Paresh Mayani kiểm tra nó ngay bây giờ sir –

Trả lời

39

Bạn cần phải sử dụng findViewById để có được một con trỏ tới hộp kiểm của bạn:

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()); 
    } 
} 

Nếu bạn đang recieving sự kiện onClick sau đó v nên không bao giờ được null nhưng tôi đã đưa vào kiểm tra thích hợp.

+0

@ Merlin đây không phải là làm việc thưa ông, hiển thị lỗi thời gian chạy của nó ... tôi sẽ rephrase câu hỏi của tôi một lần nữa .. tôi muốn rằng khi tôi bấm vào bất kỳ r ow của danh sách các hộp kiểm nên kiểm tra cho phù hợp –

+0

Tôi phát hiện ra sai lầm như nó phải có được 'R.id.Checkbox' nhưng tôi chắc chắn bạn phát hiện ra rằng ... bạn có thể nhìn vào logcat và xem những gì lỗi là – Merlin

+0

@ Merlin câu trả lời của bạn mặc dù đã cho tôi một ý tưởng gud những gì im mất tích tức là (v! = null) phần nhưng vẫn còn bị mắc kẹt, nếu tôi làm theo bạn vấn đề của tôi đã được giải quyết nhưng nó hoạt động trên khi tôi bấm hai lần bởi vì bộ chuyển đổi của tôi được tạo ra trong else khoản và nó đã mất tích hộp kiểm ... tôi đã cập nhật mã u có thể nhìn vào nó và cho tôi một số lời khuyên .. cảm ơn câu trả lời mặc dù .. –

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

không hoạt động được –

+0

Cảm ơn bạn rất nhiều: D –

7

Bên trong onListItemClick làm điều này:

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

Dưới đây là toàn bộ mã, bạn có thể thêm này sau khi cài đặt 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()); 


     } 


    }); 
Các vấn đề liên quan