• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Preselect checkboxes in a ListActivity using 'simple_list_item_checked'

daten

Lurker
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:
package org.test;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;

public class TestActivity extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayAdapter<String> list_elems = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked);
        list_elems.add("foobar");
        //TODO: check added entry labeled "foobar"
        setListAdapter(list_elems);
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
           CheckedTextView check = (CheckedTextView)v;
        check.setChecked(!check.isChecked());
    }
}
Thanks a lot in advance!

Cheers

daten
 
Back
Top Bottom