Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you have <code>CheckBox</code>es in <code>ListView</code> you have problem, because <code>ListView</code> reuses <code>View</code>s that aren't already visible on screen. That's why when you scroll down, it loads (a few times) that one <code>CheckBox</code> that was checked by you previously. So you can't rely on default <code>CheckBox</code>'s behaviour. What you need is:</p> <ol> <li><p>Prevent user from checking <code>CheckBox</code>. You can do this by calling <code>cb.setEnabled(false)</code>, where <code>cb</code> is <code>CheckBox</code> which you are using. </p></li> <li><p>Create your own adapter class that will extend <code>SimpleCursorAdapter</code>. In this class you will store <code>list</code> of indexes of items that are checked. </p></li> <li><p>Override <code>getView()</code> method in your adapter class and there manually set <code>CheckBox</code> to be checked if it's position is stored in checked items array.</p></li> <li><p>Create <code>OnClickListener</code> for your <code>ListView</code> which will take care of adding/removing checked items' positions from adapter's internal array. </p></li> </ol> <p><strong>Edit:</strong></p> <p>This would be the code of your adapter:</p> <pre><code>public class MySimpleCursorAdapter extends SimpleCursorAdapter { public ArrayList&lt;Integer&gt; mItemsChecked; public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); mItemsChecked = new ArrayList&lt;Integer&gt;(); } @Override public View getView(int position, View convertView, ViewGroup parent) { for(int i = 0; i &lt; mItemsChecked.size(); i++) { if(mItemsChecked.get(i) == position) { CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkBox1); // Instead of checkBox1, write your name of CheckBox cb.setChecked(true); } } return super.getView(position, convertView, parent); } /* Just a handy method that will be responsible for adding/removing items' positions from mItemsChecked list */ public void itemClicked(int position) { for(int i = 0; i &lt; mItemsChecked.size(); i++) { if(mItemsChecked.get(i) == position) { mItemsChecked.remove(i); return; } } mItemsChecked.add(position); } } </code></pre> <p>This is <code>OnItemClickListener</code> for your <code>ListView</code>:</p> <pre><code>final ListView lv = (ListView)findViewById(R.id.listView1); // Your ListView name lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int position, long arg3) { ((MySimpleCursorAdapter)lv.getAdapter()).itemClicked(position); lv.getAdapter().notifyDataSetChanged(); // Because we need to call getView() method for all visible items in ListView, so that they are updated } }); </code></pre> <p>Just remember to disable your <code>CheckBox</code> so that user can't click it.</p> <p>Now you can see it as a little hack, because user doesn't click <code>CheckBox</code> at all - application sees it as clicking in specific <code>ListView</code> item, which in turn automatically checks appropriate <code>CheckBox</code> for you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload