0

我有自定义适配器的列表视图,但从列表中删除项目时遇到问题。这是列表行的定义

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:descendantFocusability="blocksDescendants">

    <CheckBox
        android:id="@+id/history_list_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="10dp" 
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"/>
    
    <TextView
        android:id="@+id/history_list_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/history_list_check"/>
    
    <ImageView
        android:id="@+id/history_list_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:contentDescription="@string/history_drunk_image_description"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        />

</RelativeLayout> 

我的自定义适配器:

public class HistoryArrayAdapter extends ArrayAdapter<Measure> {
    
private final List<HistoryRowModel> list;
private final Activity context;

public HistoryArrayAdapter(Activity context, List<Measure> list) {
    super(context, R.layout.history_list_row, list);
    this.context = context;
    this.list = new ArrayList<HistoryRowModel>();
    
    for(Measure measure : list) {
        HistoryRowModel rowModel = new HistoryRowModel(measure);
        this.list.add(rowModel);
    }
}

static class ViewHolder {
    protected CheckBox checkbox;
    protected TextView text;
    protected ImageView imageView;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.history_list_row, null);
        
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.checkbox = (CheckBox) view.findViewById(R.id.history_list_check);
        viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                HistoryRowModel element = (HistoryRowModel) viewHolder.checkbox.getTag();
                element.setSelected(buttonView.isChecked());
            }
        });
        
        viewHolder.text = (TextView) view.findViewById(R.id.history_list_label);
        viewHolder.imageView = (ImageView) view.findViewById(R.id.history_list_image);

        view.setTag(viewHolder);
        viewHolder.checkbox.setTag(list.get(position));
    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
    }
    
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.checkbox.setChecked(list.get(position).isSelected());
    
    HistoryRowModel rowModel = list.get(position);
    Measure measure = rowModel.getMeasure();
    long time = measure.getTimestamp();
    DateFormat dateFormat = SimpleDateFormat.getDateInstance();
    holder.text.setText(dateFormat.format(time));
    
    int resource = getImage(measure.getThreshold());
    if(resource == -1)
    {
        holder.imageView.setImageResource(R.drawable.unknown);
    }
    else
    {
        holder.imageView.setImageResource(resource);
    }
    
    return view;
}
} 

这是我从列表中删除的代码:

private void removeSelectedRows() {
    View v;
    CheckBox cb;
    for (int i = 0; i < list.getCount(); i++) {
        v = list.getAdapter().getView(i, null, null);
        cb = (CheckBox) v.findViewById(R.id.history_list_check);
        if(cb.isChecked())
        {
            removeMeasure(i);
        }
    }
}

private void removeMeasure(long rowId) {
    
    Measure toRemove = adapter.getItem((int)rowId);
    
    try {
        Dao<Measure, Integer> measureDao = databaseHelper.getMeasureDao();
        DeleteBuilder<Measure, Integer> db = measureDao.deleteBuilder();
        db.where().eq("id", toRemove.getId());
        measureDao.delete(db.prepare());
    }
    catch(Exception e) {
        e.printStackTrace();
    }

    adapter.remove(toRemove);
}

我的问题是,当我检查某个项目然后调用 removeSelectedRows 时,它总是从列表中删除最后一个项目。然后,当我从该活动返回并再次返回时,事实证明正确的行已从数据库中删除,并且列表显示正确的列表行。所以它看起来像线

adapter.remove(toRemove);

从适配器中删除错误的项目。知道这段代码有什么问题吗?

解决了

njzk2 在他的评论中是对的

4

1 回答 1

1

用通话代替所有adapter通话list.getAdapter()。或者至少向我们展示您如何初始化adapter变量。

编辑: notifyOnChange 标志默认为“true”,因此调用以下任何适配器方法将调用同一对象的 notifyDataSetChanged() 方法:add(T), insert(T), remove(T), clear()

于 2013-10-22T21:01:19.853 回答