1

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?

belosand
  • 129
  • 3
  • 14

2 Answers2

0

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() });
Glenn
  • 175
  • 2
  • 15
  • it still doesn't work... same as before: i get the toast but nothing happens to the list view or the database... – belosand Sep 30 '13 at 20:40
  • You are deleting the row form the database, but not from the list (values) that the adapter is bound to. You must remove the item from values or rebuild values after the delete. – Glenn Sep 30 '13 at 21:09
  • Yeah, I done this already with DroidT answer. I have also another activity which shows some content from the db, so I can verify that the content also wasn't deleted from the db... Any Ideas? – belosand Sep 30 '13 at 21:15
0

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;
}
DroidT
  • 3,168
  • 3
  • 31
  • 29
  • Thanks but it still doesn't work... same as before: i get the toast but nothing happens to the list view or the database... – belosand Sep 30 '13 at 20:41
  • Does `DatabaseManidzer.COLUMN_ID = "_id"`? – DroidT Sep 30 '13 at 23:10
  • I updated my answer with the deleteVnos method that should solve your problem. The only other that could be wrong is the table name or there isn't an item in the database by that id when you try to delete it. If this solution doesn't work, as a test, try to query the database for the item before you delete it to make sure it is there and you can access it by that id. – DroidT Sep 30 '13 at 23:21
  • Hello DroidT, that didn't solve the problem eighter. So far as I now know, if I print the itemId with the Toast, I get the same id for all list entries with the same name (I display the dates as string of the entry created, and the user obviously can create multiple entries for one day...). So the problem as I see it is, I can't get the database entries ID from the listview item click (always getting the same Id for entries with the same name). any ideas? – belosand Oct 01 '13 at 13:48
  • See updated in my question the method for populating listview – belosand Oct 01 '13 at 13:50
  • Is this becouse I only get the column "delo" from my table (with together 9 columns) into the List for listview? and so the id gets missing? – belosand Oct 01 '13 at 16:35
  • Yes, this was the obvious problem, can´t belive I missed this.. I didn´t select the id value in the cursor cursorToDela() method so the arrayAdapter didn´t had any idea of the items database id, and returned only the selected names of the items while the id´s of the items with same name where the same. Thanks DroidT – belosand Oct 02 '13 at 10:52