Search code examples
androidsqliteandroid-studioandroid-cursoradapter

Delete Items from ListView in cursoradapter


i created a custom list with textbox and image. Image should delete row in database sqlite.

in my fragment activity I am using the query method of sqlitedatabase.

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

/////////////my 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();
        }
    });

however, always remove the last row in the listView. ( position of cursor is last).

I don't know how get current position from listview, when i click. Please, help me


Solution

  • Solution:

    I added code for finding position of cursor before 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();
                }
            });