【发布时间】: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