Question

I want to delete a database-entry from my ListView using a SimpleCursorAdapter. In each listitem is a button for deleting the entry. But when I click the delete button in my listview, evrytime the last item of my list will be deleted.

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

Custom 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);
    }
 }
}

Part from ContentProvider class:

@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;
}
Was it helpful?

Solution

Finally I fixed the Problem, I just need to add 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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top