0

I feel like this question was posted a thousand times. There are several cases where I get really close to the answer, but it always slips through my fingers.

What I want to do is, when I click in a row of the list, I manually un/check the CheckBox of that row.

In others words: when I click in a listView row, I have to obtain the CheckBox object associated to it so I can change the value of it.

I feel the answer is something like calling or reproducing the holder.CheckBox.setOnClickListener (which handles the CheckBox state inside the CustomAdapter) method inside the listview.setOnItemClickListener method, but I don't know how to do it :(

Just to be clear - I can get which row was clicked, which checkbox was clicked, their position and every data about it. If I click the checkBox, it gets checked no problem. The only thing I can't do is check the box when I click the text next to the box.

I can post code if requested, but as I'm not sure if it's necessary I'll avoid making this question a wall of text.

fnzr
  • 171
  • 9
  • I'd recommend using a `Checkable` row and `ListView`'s build in checklist support: http://stackoverflow.com/questions/8369640/listview-setitemchecked-only-works-with-standard-arrayadapter-does-not-work-w – CommonsWare Jul 15 '13 at 20:44

2 Answers2

0

I have an example of what you need but I have it with an image, when you click on it is changed to another:

final ImageView ui_fav = (ImageView) item.findViewById(R.id.fav_channelrow);
if (canales.get(position).getIsFav() == true) {
    ui_fav.setImageResource(R.drawable.star);
} else {
    ui_fav.setImageResource(R.drawable.silver_star);
}
ui_fav.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (canales.get(position).getIsFav()) {
            ui_fav.setImageResource(R.drawable.silver_star);
            canales.get(position).setIsFav(false);
        } else {
            ui_fav.setImageResource(R.drawable.star);
            canales.get(position).setIsFav(true);
        }
    }
});
DiegoGB90
  • 17
  • 8
0
ListView myListView  = (your listView);
myListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            ViewGroup currentItem = (ViewGroup)myListView.getChidAt(arg2);
                    (CheckBox) currentCheckBox = currentItem.findViewById(---id of the checkBox );
currentCheckBox.setChecked(true);
        }
    });
Abhishek Shukla
  • 1,242
  • 8
  • 11
  • Thanks for the answer. This does what I need it to do, but the app erases the checked signal if I scroll down and crashes if I try to check a item that was loaded when I scrolled down. I think this is solved using some sort of Tag I've seen in other answers, I'll look into it. Thanks again! – fnzr Jul 15 '13 at 22:47
  • That is because you may be using a viewHolder in your adapter.. you can set tags according to your need. – Abhishek Shukla Jul 16 '13 at 06:57
  • yes, that was it. What I lacked was understanding of how ViewGroup and ViewHolder worked. Working fine now, thanks. – fnzr Jul 16 '13 at 17:08