0

I would like to set a checkbox in a listview's click-event. How can I set the right checkbox?

This is my code:

    listView.setOnItemLongClickListener(new OnItemLongClickListener()
    {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapter, View view,
                int position, long id)
        {
                         Checkbox checkbox = ??
                         checkbox.setChecked(true);

            return true;
        }
    });
Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
Johan
  • 79
  • 9
  • check this one http://stackoverflow.com/questions/7738527/getting-an-issue-while-checking-the-dynamically-generated-checkbox-through-list/7738854#7738854 – Lalit Poptani Dec 19 '11 at 11:23

3 Answers3

1

Try out http://www.vogella.de/articles/AndroidListView/article.html may help u

Richa
  • 3,165
  • 1
  • 22
  • 26
  • 2
    Answers that contain only a link are bad answers. When the link goes offline this becomes useless. If you just want to provide a link, post as a comment instead. If you want to post an actual answer, summarize the what the "answer" in the link is and point to it for further information. Apart from that: The content on that site is *very* long. Point to a specific section that answers the question. –  Dec 19 '11 at 11:34
1

First,JavaCode in Activity:

listview.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView parentView, View childView, int position, long id)    
    {
         CheckBox cb = (CheckBox) childView.findViewById(R.id.file_checkbox);
         cb.setChecked(true);
    }
}

Second,Do not forget one thing about CheckBox in layout xml, Set android:focusable="false" to checkbox in xml , otherwise listview can`t get click event.

Third,And Most important thing,Because when listview scroll , getView() in adapter will be called unexcepted , the checkbox will be mobified unexcepted , so set checkbox status in getView() is very important , Here is my example in getView():

if(mFiles[position].isSeleted){
    checkbox.setChecked(true);
} else {
    checkbox.setChecked(false);
}
Wangchao0721
  • 909
  • 1
  • 9
  • 23
0

you need a custom Adapter base list-view you can get from :: Here

Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133