我不确定我是否理解如何使用listview.setOnLongClick ...从数据库中删除特定行...
我想使用从onLongClick“position”参数获取的位置删除db中的行,但它们彼此不对应:
列表中的位置= 1 ----> db中的位置(_id自动增量)可能是4或6,或者谁知道。
那么如何确保我在listview和db本身中都有相应的位置?
这是我的代码:
将对DBAdapter
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_NAME = "notes";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TEXT = "text";
public static final String DATABASE_NAME = "santa.db";
private static final int DATABASE_VERSION = 1;
public int InsertNote(String note) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_TEXT, note);
long success = db.insert(TABLE_NAME, null, values);
Log.d("DB", "INSERTED INTO POSITION " + success);
db.close();
return (int) success;
}
public String GetNote(int position) {
SQLiteDatabase db = this.getWritableDatabase();
String getQuery = "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_ID
+ "=" + String.valueOf(position);
// Log.d("DB", "QUERY = " + getQuery);
Cursor cursor = db.rawQuery(getQuery, null);
if (cursor != null && cursor.moveToFirst()) {
// Log.d("DB", "FIRST AND NOT NULL");
}
// Log.d("DB", "" + cursor.getCount());
String get = cursor.getString(cursor.getColumnIndex(COLUMN_TEXT));
// Log.d("GOT NOTE", "Got Note - " + get);
cursor.close();
return get;
}
// Returns amount of rows
public int GetSize() {
SQLiteDatabase db = this.getWritableDatabase();
String queryRows = "SELECT * FROM " + TABLE_NAME;
Cursor c = db.rawQuery(queryRows, null);
int size = c.getCount();
return size;
}
public Cursor GetAllNotes() {
SQLiteDatabase db = this.getWritableDatabase();
String getQuery = "SELECT * FROM " + TABLE_NAME;
Cursor cursor = db.rawQuery(getQuery, null);
return cursor;
}
public Boolean DeleteNote(int position) {
position=+1;
Boolean success = false;
SQLiteDatabase db = this.getWritableDatabase();
String deleteQuery = "DELETE FROM " + TABLE_NAME + " WHERE "
+ COLUMN_ID + "=" + position;
Log.d("DELETE", deleteQuery);
Cursor c = db.rawQuery(deleteQuery, null);
if (c.getCount() > 0) {
success = true;
}
return success;
}
// Database creation sql statement
private static final String DATABASE_CREATE = "create table " + TABLE_NAME
+ "(" + COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_TEXT + " text not null);";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + COLUMN_TEXT);
onCreate(db);
}
}
“setOnItemClickListener”,我从中获取列表中项目的位置并删除一行:
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view,
int position, long arg) {
Boolean success = db.DeleteNote(position);
if (success) {
Toast.makeText(getActivity(),
getString(R.string.deleteToast), Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(getActivity(),
getString(R.string.deleteToastFail),
Toast.LENGTH_SHORT).show();
}
mAdapter.notifyDataSetChanged();
}
});
答案 0 :(得分:0)
当您从数据库获取行时,请使用 id 列。将其存储在listview
项目的标签中,并在触发点击时从标签中读取ID。
为了做到这一点,你需要记录为对象,而不是字符串。
class Note {
private long id;
private String note;
// getters and setters
}