作者:14835688_d4705c_859 | 来源:互联网 | 2023-05-27 12:02
1> No Name..:
只需修改您的适配器类,就像这个架构我希望它可以工作:重要的是设置标签和获取标签.
public class MyAdapter extends ArrayAdapter {
private final List list;
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public MyAdapter(Activity context, List list) {
super(context, R.layout.row, list);
this.cOntext= context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (cOnvertView== null) {
LayoutInflater inflator = context.getLayoutInflater();
cOnvertView= inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView
.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here
// we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(
buttonView.isChecked()); // Set the value of
// checkbox to
// maintain its
// state.
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
}