1. Download our Official Android App: Forums for Android!

Apps Preselect checkboxes in a ListActivity using 'simple_list_item_checked'

Discussion in 'Android Development' started by daten, Sep 22, 2011.

  1. daten

    daten Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Sep 22, 2011

    Sep 22, 2011
    1
    0
    5
    Hey all,

    I'm using the ListActivity class in conjunction with the simple_list_item_checked layout which implements simple list items with checkboxes. Everything is working fine - clicking added items calls onListItemClick() and I can check/uncheck respective checkboxes of entries via the 'View v' parameter.

    However what I wasn't to figure out yet is, how to (pre)select checkboxes without any userinteraction?

    Minimal so far working code snippet showing my intend:

    PHP:
    1.  
    2. package org.test;
    3.  
    4. import android.app.ListActivity;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.widget.ArrayAdapter;
    8. import android.widget.CheckedTextView;
    9. import android.widget.ListView;
    10.  
    11. public class TestActivity extends ListActivity {
    12.     public void onCreate(Bundle savedInstanceState) {
    13.         super.onCreate(savedInstanceState);
    14.         ArrayAdapter<String> list_elems = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked);
    15.         list_elems.add("foobar");
    16.         //TODO: check added entry labeled "foobar"
    17.         setListAdapter(list_elems);
    18.     }
    19.  
    20.     protected void onListItemClick(ListView l, View v, int position, long id) {
    21.         super.onListItemClick(l, v, position, id);
    22.            CheckedTextView check = (CheckedTextView)v;
    23.         check.setChecked(!check.isChecked());
    24.     }
    25. }
    26.  
    Thanks a lot in advance!

    Cheers

    daten
     

    Advertisement

Share This Page

Loading...