2016-07-24 134 views
1

我使用文本框和圖像創建了一個自定義列表。圖像應該刪除數據庫sqlite中的行。在cursoradapter中從ListView中刪除項目

我的片段活動我正在使用sqlitedatabase的查詢方法。

cursor = mSqLiteDatabase.rawQuery("SELECT liked_id as _id, liked_presentation_id FROM liked", null); 
LikedListCursorAdapter likedListCursorAdapter = new LikedListCursorAdapter(getActivity(), cursor); 
lvItems.setAdapter(likedListCursorAdapter); 

/////////////我的CursorAdapter

public class LikedListCursorAdapter extends CursorAdapter { 

SQLiteDatabase mSqLiteDatabase; 
int idSpeaker,idPresentation; 
TextView textViewName, textViewCompany, textViewTitle, textViewDate, textViewAboutReport; 
LinearLayout linearLayout; 

    public static ImageView imageViewStatus; 
    private DatabaseHelper mDatabaseHelper; 

public LikedListCursorAdapter(Context context, Cursor cursor) { 
    super(context, cursor); 

} 


@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    return LayoutInflater.from(context).inflate(R.layout.liked_list, parent, false); 
} 

@Override 
public void bindView(View view, final Context context, final Cursor cursor) { 

    imageViewStatus = (ImageView) view.findViewById(R.id.imageViewStatus); 

    textViewTitle = (TextView) view.findViewById(R.id.textViewTitle); 
    textViewDate = (TextView) view.findViewById(R.id.textViewTime); 

    idSpeaker = cursor.getInt(cursor.getColumnIndex("_id")); 

///////////////delete item 

    imageViewStatus.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Log.w("id",String.valueOf(idSpeaker)); 
      mDatabaseHelper = new DatabaseHelper(context); 

      mSqLiteDatabase = mDatabaseHelper.getWritableDatabase(); 

      mSqLiteDatabase.delete(mDatabaseHelper.TABLE_LIKED, 
        "liked_id = ?", 
        new String[] {String.valueOf(idSpeaker)}); 
      cursor.requery(); 


      notifyDataSetChanged(); 
     } 
    }); 

然而,總是刪除列表視圖中的最後一行。 (光標的位置是最後一個)。

我不知道如何從listview中獲取當前位置,當我點擊。請幫助我

回答

0

解決方案:

我添加代碼setOnClickListener之前查找光標的位置。

final int position = cursor.getPosition(); 



imageViewStatus.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       int idSpeaker; 

       mDatabaseHelper = new DatabaseHelper(context); 
       mSqLiteDatabase = mDatabaseHelper.getWritableDatabase(); 


/////move to this position 

       cursor.moveToPosition(position); 
       idSpeaker = cursor.getInt(cursor.getColumnIndex("liked_id")); 

       Log.w("getPos",String.valueOf(cursor.getPosition())); 
       mSqLiteDatabase.delete(mDatabaseHelper.TABLE_LIKED, 
         "liked_id = ?", 
         new String[] {String.valueOf(idSpeaker)}); 
       cursor.requery(); 


       notifyDataSetChanged(); 
      } 
     }); 
0

您正在初始化您的bindView中的idSpeaker,它始終爲您提供數據的最後一個id。所以當你刪除你的表時,你的最後一個項目被刪除。

你只需要您的一個行

idSpeaker = cursor.getInt(cursor.getColumnIndex("_id"));

從綁定視圖切換到

imageViewStatus.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      cursor.moveToPosition(position); 
      idSpeaker = cursor.getInt(cursor.getColumnIndex("_id")); 

      Log.w("id",String.valueOf(idSpeaker)); 
      mDatabaseHelper = new DatabaseHelper(context); 

      mSqLiteDatabase = mDatabaseHelper.getWritableDatabase(); 

      mSqLiteDatabase.delete(mDatabaseHelper.TABLE_LIKED, 
        "liked_id = ?", 
        new String[] {String.valueOf(idSpeaker)}); 
      cursor.requery(); 


      notifyDataSetChanged(); 
     } 
    }); 
+0

它不工作。 cursor.getposition()=列表視圖中的最後一個索引 據我所知,我需要在listview中查找索引行並將其轉移到cursor.movetoposition(index); 然後將工作 –

+0

添加此 cursor.moveToPosition(位置)之前idSpeaker –