Daniel Scocco
Daniel Scocco

Reputation: 7266

Android: how to make only one item of ListView clickable?

I am working on a simple to-do list app, and on the main view I have a list of tasks with a checkmark aside to each, as the image below:

enter image description here

Upon clicking on the checkmark I want the respective task to be removed from the list. I tried to implement this as TextViews (for the tasks) and Buttons (for the checkmark), but I would need to know the number/position of the clicked checkmark (0,1,2 or 3) to remove the correct task from my array. Can I get this some how?

I also thought about implementing the tasks/checkmarks as a ListView, but then I would need to set a onItemClickListener only on the checkmark, and not on the task text. Is this possible?

Any other ideas? Thanks.

Upvotes: 0

Views: 516

Answers (2)

Marcio Covre
Marcio Covre

Reputation: 4556

If you're using a Custom Adapter, you can add a click listener on the check on getView, so the click is only on the checkbox and not on the full item.

The only problem with this is that the scroll may not work if you try to scroll by clicking on the checkbox.

Upvotes: 0

blessanm86
blessanm86

Reputation: 31779

Checkbox has a tag property which you can use to set custom data like row number. Something like this should work,

checkbox.setTag(row_number);

So when you click on it, do something like

int rowNum = Integer.parseInt(checkbox.getTag());
removeTaskAt(rowNum);

Upvotes: 2

Related Questions