ListView仅删除最后一项

时间:2013-10-22 20:41:12

标签: android android-listview

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

<?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在他的评论中是正确的

1 个答案:

答案 0 :(得分:1)

使用adapter来电替换所有list.getAdapter()来电。或者至少告诉我们如何初始化adapter变量。

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