Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

android - Custom listview with checkbox problem

I'm trying to create a custom list which will have checkboxes that allow you to select several items from the list.

The list with the checkboxes is displayed ok but if i check a checkbox and then scroll other items further down the list are also checked.

Its basically the same problem as here

I understand that it has something to do with the way android recycles the view but I cant see how to fix this! Can somebody help me???

Thanks -- Mike

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need a data structure to keep track of which rows are checked. This could be as simple as a bool[] checked.

In your getView, make sure that you set the checkbox state to the contents of checked[position]. You should also set an OnCheckedChangedListener on your check boxes in getView so that they update your data with checked[position] = isChecked.

Yes, the rows in a ListView are recycled, so make sure to populate all the appropriate data for a row before you leave getView.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...