您好我已经使用自定义适配器创建了一个List。每行都有checkbox,textView,textView。在我的适配器中,我已经使用了onCheckedChanged方法。当我在每个点击项目上放置Toast来检查状态时,它应该正常工作,但是当我想在主要活动中删除所选项目时,它总是删除我唯一的最后一个元素。有人可以给我一个提示吗?这是我的适配器代码
public View getView(final int position, final View convertView, ViewGroup parent) {
View row = convertView;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(resource, parent, false);
holder = new RowBeanHolder();
holder.txtTitle=(TextView) row.findViewById(R.id.name);
holder.indicator=(TextView) row.findViewById(R.id.indicator);
holder.checkBox=(CheckBox) row.findViewById(R.id.checkBoxDelete);
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ProductList element=(ProductList) holder.checkBox.getTag();
element.setSelected(isChecked);
//Toast.makeText(context,element.getSelected(),Toast.LENGTH_SHORT).show(); it show my current state
}
});
row.setTag(holder);
holder.checkBox.setTag(data.get(position));
}
else
{
holder = (RowBeanHolder)row.getTag();
((RowBeanHolder) row.getTag()).checkBox.setTag(data.get(position));
}
if(longPressed){
showCheckbox();
}
else{
hideCheckbox();
uncheck();
}
ProductList object = data.get(position);
holder.txtTitle.setText(object.getName());
holder.indicator.setText(object.getToBuy() + "/" + object.getBought());
holder.checkBox.setChecked(data.get(position).isSelected());
return row;
}
和我的删除操作
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()){
case R.id.action_delete:
for(ProductList p:items){
if(p.isSelected()) items.remove(p);
}
mode.finish();
return true;
default: return false;
}
}
以这种方式传递数据
adapter = new RowAdapter(context,R.layout.row,items);
listView.setAdapter(adapter);
答案 0 :(得分:0)
改变这样的逻辑,
public View getView(final int position, final View convertView, ViewGroup parent) {
View row = convertView;
RowBeanHolder holder;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(resource, parent, false);
holder = new RowBeanHolder();
holder.txtTitle=(TextView) row.findViewById(R.id.name);
holder.indicator=(TextView) row.findViewById(R.id.indicator);
holder.checkBox=(CheckBox) row.findViewById(R.id.checkBoxDelete);
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int itemPos = buttonView.getTag();
ProductList element = data.get(itemPos);
element.setSelected(isChecked);
//Toast.makeText(context,element.getSelected(),Toast.LENGTH_SHORT).show(); it show my current state
}
});
row.setTag(holder);
holder.checkBox.setTag(position);
}
else
{
holder = (RowBeanHolder)row.getTag();
((RowBeanHolder) row.getTag()).checkBox.setTag(position);
}
if(longPressed){
showCheckbox();
}
else{
hideCheckbox();
uncheck();
}
ProductList object = data.get(position);
holder.txtTitle.setText(object.getName());
holder.indicator.setText(object.getToBuy() + "/" + object.getBought());
holder.checkBox.setChecked(data.get(position).isSelected());
return row;
}