0

我想使用 SimpleCursorAdapter 从我的 ListView 中删除一个数据库条目。每个列表项中都有一个用于删除条目的按钮。但是,当我单击列表视图中的删除按钮时,每次列表中的最后一项都会被删除。

https://www.dropbox.com/s/inhdfcyxkrzrihs/problem.png

自定义 SimpleCursorAdapter:

public class BookmarksAdapter extends SimpleCursorAdapter {

private static String[] fromColumns = { Bookmarks.NAME, Bookmarks.PATH };
private static int[] toViews = { R.id.title, R.id.path };
private Cursor mCursor;
private Context mContext;
private LayoutInflater mLayoutInflater;

private int mIdIndex, mTitleIndex, mPathIndex;

public BookmarksAdapter(Context context, Cursor c, int i) {
    super(context, R.layout.item_bookmark, c, fromColumns, toViews, i);
    this.mLayoutInflater = LayoutInflater.from(context);
    this.mContext = context;
    this.mCursor = c;
    this.mIdIndex = c.getColumnIndexOrThrow(Bookmarks._ID);
    this.mTitleIndex = c.getColumnIndexOrThrow(Bookmarks.NAME);
    this.mPathIndex = c.getColumnIndexOrThrow(Bookmarks.PATH);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (mCursor.moveToPosition(position)) {
        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_bookmark,
                    parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.title.setText(mCursor.getString(mTitleIndex));
        viewHolder.path.setText(mCursor.getString(mPathIndex));
        viewHolder.remove.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO fix delete
                Uri deleteUri = ContentUris.withAppendedId(
                        Bookmarks.CONTENT_URI, mCursor.getInt(mIdIndex));
                mContext.getContentResolver().delete(deleteUri, null, null);
                update(mCursor);
            }
        });
    }
    return convertView;
}

public void createBookmark(Activity a, String path, String key) {
    Cursor c = a.getContentResolver().query(Bookmarks.CONTENT_URI,
            new String[] { Bookmarks._ID }, Bookmarks.PATH + "=?",
            new String[] { path }, null);

    if (!c.moveToFirst()) {
        ContentValues values = new ContentValues();
        values.put(Bookmarks.NAME, String.valueOf(key));
        values.put(Bookmarks.PATH, path);
        a.getContentResolver().insert(Bookmarks.CONTENT_URI, values);
        Toast.makeText(a, R.string.bookmarkadded, Toast.LENGTH_SHORT)
                .show();
    } else {
        Toast.makeText(a, R.string.bookmarkexist, Toast.LENGTH_SHORT)
                .show();
    }

    update(c);
}

@SuppressWarnings("deprecation")
private void update(Cursor c) {
    c.requery();
    notifyDataSetChanged();
}

private class ViewHolder {
    ImageButton remove;
    TextView title;
    TextView path;

    ViewHolder(View v) {
        title = (TextView) v.findViewById(R.id.title);
        path = (TextView) v.findViewById(R.id.path);
        remove = (ImageButton) v.findViewById(R.id.imageButton_remove);
    }
 }
}

ContentProvider 类的一部分:

@Override
public int delete(Uri uri, String arg1, String[] arg2) {
    int count = 0;
    switch (uriMatcher.match(uri)) {
    case BOOKMARKS:
        count = db.delete(TB_NAME, arg1, arg2);
        break;
    case BOOKMARK_ID:
        String id = uri.getPathSegments().get(0);
        count = db.delete(TB_NAME,
                _ID
                        + " = "
                        + id
                        + (!TextUtils.isEmpty(arg1) ? " AND      (" + arg1 + ')'
                                : ""), arg2);
        break;
    default:
        throw new IllegalArgumentException("Unknown URI " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return count;
}
4

1 回答 1

0

最后我解决了问题,我只需要添加getItemId(position)

@Override
public void onClick(View arg0) {
    Uri deleteUri = ContentUris.withAppendedId(Bookmarks.CONTENT_URI, getItemId(position));
    mContext.getContentResolver().delete(deleteUri, null, null);
    update(mCursor);
}
于 2014-04-05T12:18:26.167 回答