I am creating an Android app that has a ListView with each item containing a CheckBox and a TextView. I have finally managed to find out there is a bug with a CheckBox in the ListView that does not allow the setOnItemClickListener to fire, and I have managed to get the method to fire when the TextView is clicked. However, I still want the method to fire when the CheckBox is clicked.
When clicked, I want to change the value in the database to reflect it is checked (or not) so the data is saved. This part should be fine if I can simply get the method to fire when the CheckBox is clicked. According to this site, the CheckBox contains its own OnClickListener that is not allowing mine to fire, but I cannot figure out how to get around this.
The relevant code is below. Please let me know if any more code is needed.
MainActivity: Note that lv is the ListView, ItemEntity is the class that represents a single item in the ListView, and the openHelper and adapter are database objects (no problem with these). This is all inside the onCreate method.
// Fires if TextView is clicked, but not when the CheckBox is clicked
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ItemEntity item = (ItemEntity) lv.getItemAtPosition(position);
Toast.makeText(getApplicationContext(), item.toString(), Toast.LENGTH_SHORT).show();
// I want to implement this if the checkbox is clicked, but it won't even enter the outer method
if (view.getId() == R.id.my_checkbox) {
CheckBox cb = (CheckBox) findViewById(R.id.my_checkbox);
item.setChecked(cb.isChecked());
System.out.println("here " + item.toString());
openHelper.UpdateData(id, item.isChecked());
adapter.notifyDataSetChanged();
}
}
});
Aucun commentaire:
Enregistrer un commentaire