从 cursoradapter 中的 ListView 中删除项目
Delete Items from ListView in cursoradapter
我创建了一个带有文本框和图像的自定义列表。图片应删除数据库 sqlite 中的行。
在我的片段activity中,我使用的是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();
}
});
但是,始终删除列表视图中的最后一行。 (光标位置在最后)。
我不知道如何在单击时从列表视图中获取当前位置。请帮帮我
您正在 bindView 中初始化 idSpeaker
,它始终为您提供数据的最后一个 ID。因此,当您从 table 中删除时,您的最后一项将被删除。
你只需要移动你的一行
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();
}
});
解法:
我在 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();
}
});
我创建了一个带有文本框和图像的自定义列表。图片应删除数据库 sqlite 中的行。
在我的片段activity中,我使用的是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();
}
});
但是,始终删除列表视图中的最后一行。 (光标位置在最后)。
我不知道如何在单击时从列表视图中获取当前位置。请帮帮我
您正在 bindView 中初始化 idSpeaker
,它始终为您提供数据的最后一个 ID。因此,当您从 table 中删除时,您的最后一项将被删除。
你只需要移动你的一行
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();
}
});
解法:
我在 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();
}
});