我有一個自定義適配器類,如下所示。當我選擇一個複選框時,其他複選框也會在7-8行之後被選中。即使setonclicklisterner沒有實現,複選框的行爲也是一樣的。我需要一個適當的解決方案我搜索了很多,但沒有得到解決方案。 如何停止避免多選複選框?當選中一個複選框時,多個複選框會被選中?
public class CustomAdapter extends ArrayAdapter<Custom>{
private ArrayList<Custom> entries;
private Activity activity;
private Context context;
private qrusers qrusers;
private String[] udis;
ArrayList<String> userid= new ArrayList<String>();
boolean[] checkBoxState;
LayoutInflater vi;
public CustomAdapter(Context context, ArrayList<Custom> entries,String []udis) {
super(context,0,entries);
this.entries = entries;
this.context= context;
this.udis=udis;
checkBoxState=new boolean[entries.size()];
this.qrusers =(qrusers) context;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder{
public TextView item1;
public TextView item2;
public CheckBox chek;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
v = vi.inflate(R.layout.checkboxlist, null);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.contactname);
holder.item2 = (TextView) v.findViewById(R.id.companyname);
holder.chek=(CheckBox)v.findViewById(R.id.checboxlist);
holder.chek.setTag(udis[position]);
v.setTag(holder);
/* holder.chek.setOnCheckedChangeListener(new OnCheckedChangeListener() {
private String s;
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
checkBoxState[position]=true;
// isChecked=true;
s=(String)buttonView.getTag();
Log.e("IDDDDDDDD", s);
userid.add(s);
Log.e("ADDED ID", userid.toString());
}
else{
checkBoxState[position]=false;
s=(String)buttonView.getTag();
userid.remove(s);
Log.e("Removed ID", userid.toString());
}
}
});*/
holder.chek.setOnClickListener(new OnClickListener() {
private String s;
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()){
checkBoxState[position]=true;
// isChecked=true;
s=(String)v.getTag();
Log.e("IDDDDDDDD", s);
userid.add(s);
Log.e("ADDED ID", userid.toString());
}
else{
checkBoxState[position]=false;
s=(String)v.getTag();
userid.remove(s);
Log.e("Removed ID", userid.toString());
}
}
});
holder.chek.setChecked(checkBoxState[position]);
CheckBox result = (CheckBox)convertView;
if (result == null) {
result = new CheckBox(context);
}}
else
holder=(ViewHolder)v.getTag();
final Custom custom = entries.get(position);
if (custom != null) {
holder.item1.setText(custom.getcustomBig());
holder.item2.setText(custom.getcustomSmall());
}
return v;
}
public ArrayList<String> getUserid() {
return userid;
}
public void setUserid(ArrayList<String> userid) {
this.userid = userid;
}
}
嘗試使用本教程保存數組中選中的複選框的值。 http://sunil-android.blogspot.in/2013/04/android-listview-checkbox-example.html –