Question

Possible Duplicate:
Gmail-like ListView with checkboxes (and using the ActionBar)

I need :

ListView with

items { CheckBox, then TextView } ;

when you press on the CheckBox , item should change it's color.

How can I get it ?

P.S.

In other words I need a ListView with CheckBoxes like in Gmail app

Was it helpful?

Solution

The answer is quite simple! There's a component called CheckedTextView which is a combination of a TextView and a CheckBox. This component might simplify your logic, leaving you free to do modify anything you want in your list's OnItemClickListener!

I've wrote an example for you:

    public class CheckBoxListView extends ListActivity implements OnItemClickListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Mock data
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2" };

        //android's simple_list_item_multiple_choice is a CheckedTextView
        //try creating your own later ;)
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, values);

        getListView().setOnItemClickListener(this);

        setListAdapter(adapter);

    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        CheckedTextView item = (CheckedTextView) arg1;

        //The change color logic is here!
        if(item.isChecked()) {
            item.setTextColor(Color.BLACK);
            item.setChecked(false);
        }
        else {
            item.setTextColor(Color.RED);
            item.setChecked(true);
        }

    }

}

OTHER TIPS

Create custom adapter and set listeners on checkboxes that will change what you want. (I'm not posting any code until I see what you've done on your own). If you don't know how Android docs are really good place to start. For this issue you should especially check LayoutInflater, BaseAdapter, ListActivity and creating xml resources (layouts to be exact).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top