0

我想使用SimpleCursorAdapter從ListView中刪除數據庫條目。在每個listitem中都有一個用於刪除條目的按鈕。但是當我點擊列表視圖中的刪除按鈕時,evrytime列表中的最後一項將被刪除。android ContentProvider使用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; 
} 

回答

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); 
} 
相關問題