【问题标题】:Delete Items from ListView in cursoradapter在 cursoradapter 中从 ListView 中删除项目
【发布时间】:2016-11-27 19:34:02
【问题描述】:

我创建了一个带有文本框和图像的自定义列表。图片应该删除数据库 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);

////////////我的光标适配器

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 中的最后一行。 (光标位置最后)。

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

【问题讨论】:

    标签: android sqlite android-studio android-cursoradapter


    【解决方案1】:

    您正在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();
            }
        });
    

    【讨论】:

    • 它不工作。 cursor.getposition() = listview 中的最后一个索引据我了解,我需要在 listview 中找到索引行并将其传输到 cursor.movetoposition(index);然后就可以了
    • 在idSpeaker之前添加这个cursor.moveToPosition(position)
    【解决方案2】:

    解决方案:

    我在 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();
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多