嗨我正在创建一个带有两个chekboxes的自定义列表视图,问题是当我选中一个复选框并向下滚动另一个复选框旁边的复选框时也会检查我想知道它为什么会发生我应该怎么做以避免该错误这是我的代码
public class ListTestActivity extends ListActivity {
/** Called when the activity is first created. */
String tag = "Events";
String[] presidents;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an array of Strings, that will be put to our ListActivity
presidents =getResources().getStringArray(R.array.pres_array);
ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this,
getModel());
Log.d(tag," event");
setListAdapter(adapter);
Log.d(tag," 1");
}
private List<Model> getModel() {
Log.d(tag,"bht");
int index = presidents.length;
int positio;
String Linux;
//List<Model> list = new ArrayList<Model>();
/*for(positio=0;positio<=index ;++positio){
String Linux="hai";
List<Model> list = new ArrayList<Model>();
list.add(get(presidents[positio]));
list.add(get(Linux));
Log.d(tag," 2");
//if(positio>=10){
return list;
//}
}*/
List<Model> list = new ArrayList<Model>();
//int position=0;
//List<Model> list = new ArrayList<Model>();
//list.add(get(presidents[position]));
for(positio=0;positio<index ;positio++){
Linux=presidents[positio];
list.add(get(Linux));
}
Log.d(tag,"bht1");
/*list.add(get("Windows7"));
list.add(get("Suse"));
list.add(get("Eclipse"));
list.add(get("Ubuntu"));
list.add(get("Solaris"));
list.add(get("Android"));
list.add(get("iPhone"));*/
// Initially select one of the items
//list.get(1).setSelected(true);
//String re=list.get(1);
Log.d(tag," event1");
return list;
}private Model get(String s) {
Log.d(tag," event2");
return new Model(s);
}}
public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
String tag = "Events";
private final List<Model> list;
private final Activity context;
public InteractiveArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.rowbuttonlayout, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox,checkbox1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(tag," 3");
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox1 = (CheckBox) view.findViewById(R.id.checkbox1);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
Model element = (Model)viewHolder.checkbox.getTag();
String posit=element.getName();
Log.d(posit,"identifyme");
element.setSelected(buttonView.isChecked());
int dam=viewHolder.checkbox.getId();
//Log.d(dam,"bbb");
System.out.println("Checked : " + viewHolder.checkbox.getId());
}
});
viewHolder.checkbox1
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
Model element = (Model) viewHolder.checkbox1.getTag();
String posit=element.getName();
Log.d(posit,"identify");
element.setSelected(buttonView.isChecked());
//Log.d(tag,"identifyme");
}
});
view.setTag(viewHolder);
Log.d(tag,"me");
viewHolder.checkbox.setTag(list.get(position));
viewHolder.checkbox1.setTag(list.get(position));
} else {
view = convertView;
Log.d(tag,"meeee");
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
((ViewHolder) view.getTag()).checkbox1.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
//String Dis=list.get(position).getName();
//Log.d(Dis," event7");
holder.checkbox.setChecked(list.get(position).isSelected());
holder.checkbox1.setChecked(list.get(position).isSelected());
Log.d(tag," event7");
return view;
}
}
public class Model {
String tag = "Events";
private String name;
private boolean selected;
public Model(String name) {
this.name = name;
selected = false;
Log.d(tag," ev");
}
public String getName() {
Log.d(tag," eve");
return name;
}
public void setName(String name) {
Log.d(tag," nt");
this.name = name;
}
public boolean isSelected() {
Log.d(tag," t");
return selected;
}
public void setSelected(boolean selected) {
Log.d(tag," eventer");
this.selected = selected;
}
}
答案 0 :(得分:1)
mViewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox1);
mViewHolder.checkBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox1);
if (cb.isChecked()) {
checkBoxArrayList.set(position, true);
} else if (!cb.isChecked()) {
checkBoxArrayList.set(position, false);
}
}
});
您好以下链接可能对您有所帮助 http://mylearnandroid.blogspot.in/2014/06/listview-problem-while-scrolling.html
答案 1 :(得分:0)
此链接也存在同样的问题,并在以下链接中解决。
Storing the state of checkbox in listview while using custom BaseAdapter in android?
希望有所帮助