gpt4 book ai didi

android listview,复选框随机变化

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:58:38 27 4
gpt4 key购买 nike

我有一个每行都有复选框的 ListView ,当我选择任何复选框然后滚动屏幕时,每个项目的复选框都会随机更改,我的意思是取消选中它变成选中并且有时它保持取消选中状态,然后当我滚动时再次起来,勾选的变成取消勾选了,我发现了这个问题Android listview with checkbox problem , 用户说这对他们有帮助,但我不明白我应该做什么,请帮助

这是我的适配器

class RestaurantAdapter extends BaseAdapter {
private static LayoutInflater inflater = null;
private ArrayList<HashMap<String, String>> data;
Activity activity;

public RestaurantAdapter(Activity activity,
ArrayList<HashMap<String, String>> data) {
// TODO Auto-generated constructor stub
this.activity = activity;
this.data = data;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}

@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
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.restaurant_multi_select_list_item,
null);
TextView name = (TextView) vi
.findViewById(R.id.restaurant_multi_select_title);
ImageView image = (ImageView) vi
.findViewById(R.id.restaurant_multi_select_list_item_image);
CheckBox cb = (CheckBox) vi
.findViewById(R.id.restaurant_multi_select_checkBox);
HashMap<String, String> restaurant = data.get(position);
name.setText(restaurant.get("name"));

image.setImageResource(Integer.parseInt(restaurant.get("image")));
return vi;

}
}

最佳答案

关于循环和回收listview中的元素,看看converView的概念。

我编辑了你的代码并添加了一个状态数组来保存每个复选框的最后一个值并在每个循环和回收中加载它,

class RestaurantAdapter extends BaseAdapter {
private ArrayList<Boolean> status = new ArrayList<Boolean>();
private static LayoutInflater inflater = null;
private ArrayList<HashMap<String, String>> data;
Activity activity;

public RestaurantAdapter(Activity activity,
ArrayList<HashMap<String, String>> data) {
// TODO Auto-generated constructor stub
this.activity = activity;
this.data = data;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < data.size(); i++) {
status.add(false);
}
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}

@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(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.restaurant_multi_select_list_item,
null);
TextView name = (TextView) vi
.findViewById(R.id.restaurant_multi_select_title);
ImageView image = (ImageView) vi
.findViewById(R.id.restaurant_multi_select_list_item_image);
CheckBox cb = (CheckBox) vi
.findViewById(R.id.restaurant_multi_select_checkBox);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
status.set(position, true);
} else {
status.set(position, false);
}
}
});
cb.setChecked(status.get(position));
HashMap<String, String> restaurant = data.get(position);
name.setText(restaurant.get("name"));

image.setImageResource(Integer.parseInt(restaurant.get("image")));
return vi;

}
}

不是说你必须在 getview 上使用 setOnCheckedChangeListener,而不是在 listview 上

关于android listview,复选框随机变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14468852/

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