Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get proper view in ListActivity to set OnCheckedChangeListener for CheckBox widget in Android
    primarykey
    data
    text
    <p>I have a ListActivity which will display three widgets for each row. One of them is a checkbox.</p> <pre><code>public class GroceryItems extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.grocery_items_list); setUpData(); } </code></pre> <p>In my setUpData() I'm using a SimpleAdapter (this is all a test case).</p> <pre><code>private void setUpData(){ // generate the maps String[] from = new String[] {"checked", "item_name","quantity"}; int[] toDisplay = new int[] {R.id.checkbox1, R.id.text1, R.id.text2}; List&lt;HashMap&lt;String, Object&gt;&gt; fillMaps = new ArrayList&lt;HashMap&lt;String, Object&gt;&gt;(); for(int i = 0; i &lt; 20; i++){ HashMap&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;(); if(i==3){ map.put("checked", false); } else{ map.put("checked", true); } map.put("item_name", items[i]); map.put("quantity","3"); fillMaps.add(map); } ListView lv= getListView(); SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.common_rowitem, from, toDisplay); lv.setAdapter(adapter); CheckBox cb = (CheckBox) lv.findViewById(R.id.checkbox1); cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton cb, boolean isChecked) { if(cb.isChecked()) { // action } else { // action } } }); } </code></pre> <p>On findViewById I always get a null returned and can't add the listener. I have also tried to use a view binder</p> <pre><code> adapter.setViewBinder(new SimpleAdapter.ViewBinder() { public boolean setViewValue(View view, Object data, String textRepresentation) { if (view.getId()==R.id.checkbox1){ CheckBox cb = (CheckBox) view; if (data != null){ cb.setChecked((Boolean)data); return true; } } return false; } }); </code></pre> <p>This also fails to install the handler.</p> <p>Here are the two layout views</p> <p>grocery_items_list.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; </code></pre> <p></p> <pre><code>&lt;ListView android:id="@+id/android:list" android:layout_width="wrap_content" android:layout_height="wrap_content"/&gt; </code></pre> <p> </p> <p>common_rowitem.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; </code></pre> <p> </p> <p></p> <p>Any suggestions or help is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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