您必须根据为列表项分配颜色来维护每个列表视图项的状态。
更新-
对于列表中的复选框,我也发生了同样的问题。它会自动更改。
public class ListAdapter extends BaseAdapter{
private List<String> mName;
private List<Drawable> mIcon;
private Context mContext;
private LayoutInflater mInflater;
private DataHelper mDataHelper;
private List<String> DBappName;
boolean[] checkBoxState;
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
public ListAdapter(Context mContext, List<String> Name, List<Drawable> appIcon, ) {
this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mContext=mContext;
this.mName=Name;
this.mIcon=Icon;
mDataHelper=new DataHelper(mContext);
DBappName=new ArrayList<String>();
DBappName=mDataHelper.selectName();
/***Initialization***/
for (int i = 0; i < this.getCount(); i++) {
itemChecked.add(i, false); // initializes all items value with false
}
for (int i = 0; i < DBappName.size(); i++) {
for(int j=0;j<mName.size();j++){
if(DBappName.get(i).equals(mName.get(j))){
itemChecked.add(j, true);
}
}
}
}
@Override
public int getCount() {
return mName.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder mHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_menu, null);
mHolder = new ViewHolder();
mHolder.mText=(TextView) convertView.findViewById(R.id.Name);
mHolder.mImage=(ImageView) convertView.findViewById(R.id.Icon);
mHolder.mCheckBoxLock=(CheckBox) convertView.findViewById(R.id.mCheckbox);
convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder) convertView.getTag();
}
mHolder.mText.setText(mName.get(position));
mHolder.mImage.setImageDrawable(mIcon.get(position));
mHolder.mCheckBoxLock.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
itemChecked.set(position, true);
mDataHelper.insert(mName.get(position));
}else{
itemChecked.set(position, false);
mDataHelper.delete(mName.get(position));
}
}
});
mHolder.mCheckBoxLock.setChecked(itemChecked.get(position)); // this will Check or Uncheck the
return convertView;
}
private class ViewHolder {
private TextView mText;
private ImageView mImage;
private CheckBox mCheckBoxLock;
}
}