我尝试了几种方法从项目长按点击列表视图中删除记录,但没有任何反应,没有错误,没有删除任何东西,只是吐司显示... 这是代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.layout.push_left_in, R.layout.push_left_out);
setContentView(R.layout.moje_ure);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
datasource = new VnosiDataSource(this);
datasource.open();
final List<VnosiDB> values = datasource.getAllDela();
final ArrayAdapter<VnosiDB> adapter = new ArrayAdapter<VnosiDB>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
ListView ureList = getListView();
adapter.notifyDataSetChanged();
ureList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int pos,
long id) {
// TODO Auto-generated method stub
//some code here...
String posit = values.get(pos).toString();
Toast.makeText(Ure.this, posit, Toast.LENGTH_SHORT).show();
}
});
ureList.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view,
int pos, long id) {
// TODO Auto-generated method stub
datasource.deleteVnos((int)values.get(pos).getId());
Toast.makeText(Ure.this, "Vnos " + values.get(pos).toString() + " izbrisan!", Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
return true;
}
});
数据库的删除方法:
public void deleteVnos(int _id){
database.delete(DatabaseManidzer.TABLE_VNOSI, DatabaseManidzer.COLUMN_ID + " = " + _id, null);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
UPDATE:在VnosiDataSource.java类中填充列表视图的方法:
public List<VnosiDB> getAllDela() {
List<VnosiDB> dela = new ArrayList<VnosiDB>();
Cursor cursor = database.rawQuery(
"SELECT delo from vnosi ORDER BY vnos DESC", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
VnosiDB curdela = cursorToDela(cursor);
dela.add(curdela);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return dela;
}
我错过了什么?
答案 0 :(得分:0)
db.delete需要3个参数 - 来自参考
public int delete(String table,String whereClause,String [] whereArgs)
所以你的代码应该是这样的:
db.delete(TableName, "Id=?" ,
new String[] { Id.toString() });
答案 1 :(得分:0)
删除项目后,需要再次重新查询数据库。获得结果后,再次使用结果设置适配器。
@Override
protected void onCreate(Bundle savedInstanceState) {
....
updateListView();
ureList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int pos,
long id) {
// TODO Auto-generated method stub
//some code here...
String posit = values.get(pos).toString();
Toast.makeText(Ure.this, posit, Toast.LENGTH_SHORT).show();
}
});
ureList.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View view,
int pos, long id) {
VnosiDB item = (VnosiDB) getListAdapter().getItem(pos);
int itemId = item.getId();
datasource.deleteVnos(itemId);
Toast.makeText(Ure.this, "Vnos " + item.toString() + " izbrisan!", Toast.LENGTH_SHORT).show();
updateListView();
return true;
}
});
public void updateListView() {
final List<VnosiDB> values = datasource.getAllDela();
final ArrayAdapter<VnosiDB> adapter = new ArrayAdapter<VnosiDB>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
public void deleteVnos(long itemId){
database.delete(DatabaseManidzer.TABLE_VNOSI, "_id = ?", new String[]{Long.toString(itemId)});
}
<强>更新强>
在查询数据库中的项目时,您没有选择_id
。我不确定你在cursorToDela(游标)中做了什么,但你没有在该游标中使用_id
来填充curdela.getId();
public List<VnosiDB> getAllDela() {
List<VnosiDB> dela = new ArrayList<VnosiDB>();
Cursor cursor = database.rawQuery(
"SELECT _id, delo from vnosi ORDER BY vnos DESC", null);
while (cursor.moveToFirst()){
//cursorToDela needs to grab the _id from the cursor and set it on the created VnosiDB.
VnosiDB curdela = cursorToDela(cursor);
dela.add(curdela);
}
// Make sure to close the cursor
cursor.close();
return dela;
}