简体   繁体   中英

Android: Can't delete record from ListView and SQLite

I've tried several methods of deleting the record from a listview on item long click but, nothing happens, no errors, no delete nothing, just the toast shows up... Here is the code:

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

And the delete method for the database:

public void deleteVnos(int _id){

    database.delete(DatabaseManidzer.TABLE_VNOSI, DatabaseManidzer.COLUMN_ID + " = " + _id, null);
} 

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

UPDATE: Method for populating the list view in class 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;
}

What am I missing?

db.delete requires 3 parameters - this from the reference

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

So your code should look something like this:

   db.delete(TableName, "Id=?" ,
      new String[] { Id.toString() });

You need to requery the database again after you delete the item. Once you get the results back set the adapter again with the results.

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

UPDATE
You aren't selecting the _id when you are querying the database for your items. I'm not sure what you are doing in cursorToDela(cursor) but you don't have the _id in that cursor to populate what is returned by 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM