I’m making my first Android application and I’m having a problem for which I can’t find the answer anywhere on Google.
I want a list of items with checkboxes. I want both the item itself and the checkbox to be clickable.
public class MyItem extends ListActivity {
private ArrayList<MyItem> items;
public void onCreate(Bundle savedInstanceState) {
/* code which creates instances of MyItem and inserts them on the *list* variable */
MyArrayAdapter adapter = new MyArrayAdapter(this, R.layout.my_item, list);
setListAdapater(adapter);
setContentView(R.layout.items_list);
}
public onListItemClick(ListView l, View v, int position, long id){
//handles the click on an item
}
public class MyArrayAdapter extends ArrayAdapter<MyItem>{
private MyItem item;
public MyArrayAdapter(Context context, int resourceId, ArrayList<MyItem> list){
//code for the constructor
}
public getView(int position, View convertView, ViewGroup parent){
convertView = inflater.inflate(resourceId, null);
this.item = list.get(position);
if (this.item == null) {
return convertView;
}
else{
if (resourceId == R.layout.my_item) {
final CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkbox);
if(cb != null){
//initially
if(chosen)
cb.setChecked(true);
else
cb.setChecked(false);
//set listener
cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(cb.isChecked())
chosen = true;
else
chosen = false;
}
});
}
}
return convertView;
}
}
}
Don’t worry about the chosen variable. I wrote that to simply the code. It actually corresponds to a value in a database. The clicking on an item works just fine. However when I click on a checkbox what happens is this:
- the checkbox in which I clicked appears selected (this is the work of the Android’s UI)
- the checkbox that internally is checked is the last one on the screen whichever it is, i.e., if I my screen displays 8 items and I click in one of them (doesn’t matter which one) the check appears in the correct checkbox but internally, the 8th item is the one getting checked.
I would appreciate any help you could provide me. Thanks in advance.
Actually, the implementation backing
chosenis key. Android does some optimization with list views to allow you to reuse the list item views to avoid excessive object creation and garbage collection (which would often lead to jerky scrolling). As such, you have to make sure that whenever relevant, you know exactly which list item you’re working with.Let’s say that you have a 100 list items. Your screen is probably not going to be able to display all of them. You might only show ten items at a time. So 10 views (really view hierarchies) are created to display those visible items. When you scroll down to the next ten items, instead of creating 10 new views (for a total of 20), the list might only create one more (to cover the case where half of one item is showing at the top and half of one is showing at the bottom of the screen for a total of 11 items visible on the screen) and the rest of items reuse the views created before.
So a conceptual table representing the first screen might look like this:
And for after scrolling down ten items, it might look a little like this (probably not exactly, but this gets you the idea):
So what you can gather from this is that a single given view can represent different items as you scroll around. This means that your event handlers have to be a little more dynamic in how they find the item they’re related to.
All this is to give you a bit of background so that you can change how you’re implementing your
getViewmethod. Here’s your actual problem: the variableitemis in the scope of yourAdapter. Unfortunately, I’m guessing that your code that you haven’t posted here that you’ve replaced withchosenusesitem. You setitemwhenever an item view gets created. This means that after those first 8 views are created,itemis set to the 8th item in your list. Whenever you click on a checkbox, you’re usingitemwhich is the 8th item and not the item that corresponds to the list item view that you clicked.Here’s the structure for
getViewthat I’d recommend:Note that I’ve gotten rid of the class-level
itemvariable.