我想按位置删除行。我写了这个,但它不起作用。现在它是按 id 删除行,而不是按位置。我不知道如何编写该查询。
例如,这是我的表:
position | name
1 | aaaa
2 | bbbb
3 | cccc
4 | dddd
现在我要删除第 3 行。我想要一个像这样的表格:
position | name
1 | aaaa
2 | bbbb
3 | dddd
这是从主 Activity 类中删除方法:
public void onClickBtnDelete(View view) {
SparseBooleanArray checkedItemPositions = getListView()
.getCheckedItemPositions();
int itemCount = getListView().getCount();
int[] tab = new int[itemCount];
for (int i = itemCount - 1; i >= 0; i--){
tab[i] = itemCount - i;
if (checkedItemPositions.get(i)) {
adapter.remove(list.get(i));
db.deleteMyPlace(new MyPlacesTable(tab[i]));
}
}
这是我从 DatabaseHandler 中删除方法:
public void deleteMyPlace(MyPlacesTable myPlace) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_MY_PLACES, KEY_ID + " = ?",
new String[] { String.valueOf(myPlace.getID()) });
db.close();
}
以及构造函数、getter 和 setter:
public MyPlacesTable(int id){
this._id = id;
}
public int getID(){
return this._id;
}
public void setID(int id){
this._id = id;
}
你能帮我吗?
最佳答案
试试这个
public static void DeleteData(SQLiteDatabase db, String TableName,String userfield,String userid)
{
String table_name = TableName;
String where =userfield + "='" +userid+"'";
String whereArgs[] = null;
db.delete(table_name, where, whereArgs);
}
关于java - 如何按位置从数据库中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24383597/