1

我已经尝试了几种方法从列表视图中删除记录的项目长按但是,没有任何反应,没有错误,没有删除什么,只是吐司出现......这是代码:

@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;
}

我错过了什么?

4

2 回答 2

0

db.delete 需要 3 个参数 - 这来自参考

public int delete (String table, String whereClause, String[] whereArgs)

所以你的代码应该是这样的:

   db.delete(TableName, "Id=?" ,
      new String[] { Id.toString() });
于 2013-09-30T20:00:22.303 回答
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(cursor) 中做了什么,但你没有_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;
}
于 2013-09-30T20:03:31.310 回答