我的 ListView 有一个 BaseAdapter,在一个片段中,如下所示:
public class SelectionMucListAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
Typeface titleFace;
ArrayList<UsersData> innerList = new ArrayList<UsersData>();
public SelectionMucListAdapter(ArrayList<UsersData> users) {
inflater = (LayoutInflater) mainActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
titleFace = Typeface.createFromAsset(mainActivity.getAssets(), "fonts/bradybun.ttf");
innerList = users;
}
@Override
public int getCount() {
return innerList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int id) {
return id;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.muc_list_item, null);
viewHolder = new ViewHolder();
viewHolder.image = (ImageView)convertView.findViewById(R.id.item_image);
viewHolder.check = (CheckBox)convertView.findViewById(R.id.cb_choose_user);
viewHolder.title = (TextView)convertView.findViewById(R.id.item_text_1);
viewHolder.rating=(RatingBar)convertView.findViewById(R.id.item_text_2);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.title.setTypeface(titleFace);
Bitmap avatar = innerList.get(position).getUserAvatar();//UsersManager.getInstance().getUsers().get(position).getUserAvatar();
if(avatar != null)
viewHolder.image.setImageBitmap(avatar);
viewHolder.title.setText(innerList.get(position).getUserName());
viewHolder.rating.setRating((float)Double.parseDouble(innerList.get(position).getRating()));
viewHolder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
if(cb.isChecked()) {// if this item is checked then add to list
Log.v("CheckBox", "pos: "+position+" checked");
if(!choices.contains(innerList.get(position))) {
choices.add(innerList.get(position));
}
} else {// if its unchecked then remove from list if exist
Log.v("CheckBox", "pos: "+position+" checked");
if(choices.contains(innerList.get(position))) {
choices.remove(innerList.get(position));
}
}
isChecked = cb.isChecked();
}
});
viewHolder.check.setTag(position);
return convertView;
}
}
static class ViewHolder{
ImageView image;
CheckBox check;
TextView title;
RatingBar rating;
}
class CheckState{
boolean isChecked;
UsersData usersData;
public UsersData getUsersData() {
return usersData;
}
public void setUsersData(UsersData usersData) {
this.usersData = usersData;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
}
我有 20 个列表项,问题是当我检查前 2 个项目时,例如在索引 [0] 和 [1] 处,不知何故,当我向下滚动时,它还会自动检查索引 [9] 和 [10] 处的其他项目列表。如果我检查其他项目也会发生同样的情况,并且每次我向下滚动列表时它都会以相同的模式重复检查。
我实际上不知道发生了什么,所以我想问一下这里是否有人可以阻止这种混乱。谢谢!