gpt4 book ai didi

android - android中带有复选框错误的自定义 ListView

转载 作者:行者123 更新时间:2023-11-29 02:09:13 25 4
gpt4 key购买 nike

您好我正在创建一个带有两个复选框的自定义 ListView ,问题是当我选中一个复选框并向下滚动与选中的复选框相邻的另一个复选框时也被选中我想知道为什么会发生我应该怎么做才能避免该错误这是我的代码

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;
}

}

最佳答案

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

关于android - android中带有复选框错误的自定义 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8427043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com