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;
        }
    });
1

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.