我在Gridview中有几个TextViews,ImageViews和复选框。我的复选框正常工作,我可以检查和取消选中,但问题是这是多选。如果我选中第一个复选框并向下滚动,我会看到第4个,第7个,第10个项目复选框也会被检查。我已经阅读了很多,我相信这是因为循环视图。我不知道如何解决这个问题。我只想点击第1和第6个复选框,只检查这两个框。下面是我的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;
}
}
");
答案 0 :(得分:2)
您还必须从holder
设置复选框的选中状态。否则,已检查状态将是循环视图的状态。
我必须说,你实现了所有这一切,但是按照你已有的编码,这样的事情应该有效:
在你的活动中,定义一个成员变量来记住当前检查的项目(注意,如果活动被销毁/重新创建但是这里的范围超出范围,你必须保存/恢复该变量的值) :
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);
}