从cursoradapter中的ListView中删除项目

时间:2016-07-24 10:41:30

标签: android sqlite android-studio android-cursoradapter

我创建了一个包含文本框和图像的自定义列表。图像应删除数据库sqlite中的行。

在我的片段活动中我正在使用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();
        }
    });

但是,始终删除listView中的最后一行。 (光标的位置是最后的。)

当我点击时,我不知道如何从列表视图中获取当前位置。拜托,帮帮我

2 个答案:

答案 0 :(得分:1)

您正在初始化bindView中的Microsoft Visual Studio A reference to a higher version or incompatible assembly cannot be added to the project ,它始终为您提供数据的最后ID。因此,当您从表中删除时,您的最后一项将被删除。

您只需要移动一行

idSpeaker

从绑定视图到

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

答案 1 :(得分: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();
            }
        });