我使用文本框和圖像創建了一個自定義列表。圖像應該刪除數據庫sqlite中的行。在cursoradapter中從ListView中刪除項目
我的片段活動我正在使用sqlitedatabase的查詢方法。
cursor = mSqLiteDatabase.rawQuery("SELECT liked_id as _id, liked_presentation_id FROM liked", null);
LikedListCursorAdapter likedListCursorAdapter = new LikedListCursorAdapter(getActivity(), cursor);
lvItems.setAdapter(likedListCursorAdapter);
/////////////我的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中獲取當前位置,當我點擊。請幫助我
它不工作。 cursor.getposition()=列表視圖中的最後一個索引 據我所知,我需要在listview中查找索引行並將其轉移到cursor.movetoposition(index); 然後將工作 –
添加此 cursor.moveToPosition(位置)之前idSpeaker –