Cg2916
Cg2916

Reputation: 1117

Add Checkbox to ListView Item in Android

I'm trying to add a CheckBox to every item in my ListView. I can't put a CheckBox widget in the ListView, so how do I do it? I think the Android settings has something like this.

Upvotes: 2

Views: 5801

Answers (2)

Hakan Ozbay
Hakan Ozbay

Reputation: 4719

Off the top of my head, you should create an xml layout file representing an item in the ListView, in which you would declare a CheckBox. You can then set the adapter to use your layout for every item as follows:

ArrayAdapter<T> adapter = new ArrayAdapter<T>(this, R.layout.list_item, listReference); 
setListAdapter(adapter);

setListAdapter is a method in ListActivity. Your list_item.xml should have the CheckBox defined in it.

Upvotes: 5

David K.
David K.

Reputation: 6361

You need to extend a view adapter class and provide your own logic for creating the views for each row. Here's an excerpt from an android development book at commonsware.com that might help.

Upvotes: 1

Related Questions