Android - Get item from listview with custom adapter through checkbox

832 views Asked by At

I have a listview with a custom adapter (extends BaseAdapter).

It recieves a list of objects I have to populate a ListView.

One of my object's atribute is a boolean called "checked".

on my method getView, this atribute is responsible for checking or not checking a CheckBox on my View.

Everything is working just fine and when my Activity loads, the ListView itens apear as they are on my list of objects (which was received from my database), some checked and some not checked.

But when I check one of my ListView's checkbox, I need to update my object and therefore it's value on my database. The problem is:

"How do I know which item (object) I have to update just by checking my CheckBox?"

"Don't they have the same name?"

I have a listView.setOnItemClickListener(...) where I can get my object by it's position, but it works when I click on the "row" of my list view itself, not on my checkbox... I thought about using it to check/uncheck my CheckBox... But how would I do that? Can I use the position to get a specific CheckBox from my listView?

In the end, I also thought that the best method would use the "listView.setOnItemClickListener(...)" to check my CheckBox, once it would be easier for my user to check one Item by it's row than by a tiny CheckBox, so can someone help me with the best way to solve my problem?

I'm sorry I didn't post my code, but right now I can't access it.

1

There are 1 answers

1
EE66 On BEST ANSWER

Take a look at this tutorial

You need to add an method in the onClick of the checkbox that will be implemented by the activity. One way to do that is to add an abstract method to the adpater and make the activity implement it. lets call onCheckBoxClicked(int position)