gpt4 book ai didi

Gridview 回收 View 问题中的 Android CheckBox

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

我在 Gridview 中有几个 TextView、ImageView 和复选框。我的复选框工作正常,我可以选中和取消选中,但问题是这是多选的。如果我选中第一个复选框并向下滚动,我会看到第 4、第 7、第 10 个项目复选框也被选中。我已经阅读了很多,我相信这是因为回收 View 。我不确定如何解决这个问题。我想要的是,如果我单击第一个和第六个复选框,则只应选中这两个框。下面是我的 BaseAdapter 代码。我是 android 的新手,请帮助我。

public class test extends BaseAdapter {
public int itemSelected = 0;


private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
SparseBooleanArray checked;
HashMap<String, String> compareSelectionList1 = new HashMap<String, String>();

public test(Activity a, ArrayList<HashMap<String, String>> d) {

activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return data.size();
}

@Override
public Object getItem(int position) {
//return position;
return data.get(position);
}

@Override
public long getItemId(int position) {
return position;
//return data.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater1 = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater1.inflate(R.layout.comparison_selection_list_row_del, parent, false);

holder = new ViewHolder();
holder.txtBrandName = (TextView) convertView.findViewById(R.id.txtBrandsn);
holder.prodImg = (ImageView) convertView.findViewById(R.id.btnSmallImage);
holder.recFlagImg = (ImageView) convertView.findViewById(R.id.btnRecFlag);

holder.txtPartNumber = (TextView) convertView.findViewById(R.id.txtpartnumber);
holder.txtNotes = (TextView) convertView.findViewById(R.id.txtnotes);

holder.chkBox = (CheckBox) convertView.findViewById(R.id.chkBox);
holder.chkBox.setId(position);

convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
}


compareSelectionList1 = data.get(position);
holder.chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
holder.chkBox.setBackgroundResource(R.drawable.compselection_checked_btn);
} else {
holder.chkBox.setBackgroundResource(R.drawable.compselection_btn);
}
}
});

holder.txtBrandName.setText(compareSelectionList1.get("brands"));


holder.txtPartNumber.setText(compareSelectionList1.get("partnumbers"));
holder.txtNotes.setText(compareSelectionList1.get("footnotes"));
holder.txtNotes.setClickable(true);
holder.txtPartNumber.setClickable(true);
holder.id = position;

return convertView;
}

}

");

最佳答案

您还必须从您的holder 设置复选框的选中状态。否则,检查状态将是回收 View 的状态。

我必须说,您实现所有这些有点笨拙,但是根据您已有的代码进行编码,这样的事情应该可行:

在您的 Activity 中,定义一个成员变量以记住当前检查了哪些项目(请注意,您必须保存/恢复该变量的值,以防 Activity 被销毁/重新创建,但这超出了此处的范围) :

Set<Long> mCheckedItemPositions = new HashSet<Long>();

像这样扩展您的 OnCheckedChangeListener,以跟踪检查的位置:

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
holder.chkBox.setBackgroundResource(R.drawable.compselection_checked_btn);
// you use the ID field to hold the position
mCheckedItemPositions.add(buttonView.getId());
} else {
holder.chkBox.setBackgroundResource(R.drawable.compselection_btn);
mCheckedItemPositions.remove(buttonView.getId());
}

最后在 getView 方法的末尾添加它,就在 return 语句之前,以便根据 mCheckedPositions< 以正确的状态呈现复选框:

if (mCheckedPositions.contains(holder.chkBox.getId()) {
holder.chkBox.setBackgroundResource(R.drawable.compselection_checked_btn);
} else {
holder.chkBox.setBackgroundResource(R.drawable.compselection_btn);
}

关于Gridview 回收 View 问题中的 Android CheckBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20776734/

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