我已经尝试了几种方法从列表视图中删除记录的项目长按但是,没有任何反应,没有错误,没有删除什么,只是吐司出现......这是代码:
@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();
}
更新:在类 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;
}
我错过了什么?