public class AppleSupportList extends BaseAdapter {
private LayoutInflater mInflater;
private final String[] values;
public AppleSupportList(Context context, String[] values) {
mInflater = LayoutInflater.from(context);
this.values = values;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return values.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.customlist1, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.labelContact1);
holder.img = (ImageView) convertView.findViewById(R.id.imageContact1);
holder.check = (CheckBox) convertView.findViewById(R.id.checkBoxContact1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(values[position]);
holder.check.setId(position);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.check.isChecked()) {
holder.check.setChecked(false);
//store id
} else {
holder.check.setChecked(true);
//remove id
}
}
});
holder.check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
if (cb.isChecked()) {
//store id
} else {
//remove id
}
}
});
return convertView;
}
static class ViewHolder {
TextView text1;
ImageView img;
CheckBox check;
}
}
我正在创建一个带有复选框的自定义列表视图。所有工作正常,除非我滚动列表时复选框自动选中/取消选中。我如何存储复选框的状态。我已经解决了所有相关问题,但对我没有任何帮助。请看一下我的代码并帮助我。