0

I know this question has been asked alot, but many of the answers I have found have been unsatisfactory.

I have a Baseadapter which displays a list via a database. The information is passed from the database to the list via a cursor, which adds the cursor to an arraylist, which then populates the listview. I would like to delete a listitem via a contextmenu and have it deleted from both the listview AND the database. Currently, I am using adaptercontextmenuinfo object to get the position and/or id which I pass to a delete method in the database class, but the info.id is not corresponding with the database _id. Currently I am able to successfully remove the row entry from the listadapter, but NOT from the database. Any help would be much appreciated. (note: my database has 3 columns, the first of which being _id) ContextMenu java:

@Override
    public boolean onContextItemSelected(MenuItem item) {
        if(item.getTitle() == "Delete"){ //if "delete" is selected
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

            dba.deleteRow(info.id);
            DATA.remove(info.position);
            adapter.notifyDataSetChanged();

Database delete row method:

public void deleteRow(long rowId){
        db = dbhelper.getWritableDatabase();
        try{
            db.delete(Constants.TABLE_NAME, Constants.KEY_ID + "="+rowId,null);
        }catch(Exception e){
        }
    }

I know there is alot of code involved here. If you want more, let me know. Thanks for your help!

benbeel
  • 1,532
  • 4
  • 24
  • 38

2 Answers2

2

Comparing of strings in Java is done by .equals() or equalsIgnoreCase() methods, not with == Which might be the cause your statement not being reachable. If you do "this"=="that" means you are checking if both of them are of same reference. (yes its reachable, since both of them are strings :P )

Make sure your database object is not null and also check if any exception is caught. Plus: Try removing the data also from the cursor.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • Thanks. The if statement is not the issue, that code executes appropriately. The issue is passing the _id of the sqlite database row. I know that info.id is returning the row number in the listview, which does not necessarily = _id of the sqlite db – benbeel Dec 25 '11 at 22:05
  • But if you have more if-else statements will cause you all of the statements reachable. – Nikola Despotoski Dec 25 '11 at 22:19
  • I think you dont use the id from your database, but the id of the listview? – tobias Dec 25 '11 at 23:59
  • @nikola: the code is being reached, I have confirmed that via debugging logs. – benbeel Dec 26 '11 at 02:51
  • http://stackoverflow.com/questions/6819604/comparison-of-two-strings-doesnt-work-in-android/ this is what i mean and talking about :) Yes it is reachable, I have no argument about that. :) – Nikola Despotoski Dec 26 '11 at 02:53
  • I see what you're saying about the strings, its a good point. I figured out the answer, see below. – benbeel Dec 26 '11 at 05:05
0

I figured out the answer. Here it is:

        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        Cursor c = dba.getsavedcontacts();
        c.moveToPosition(info.position);
        String id = c.getString(c.getColumnIndex(Constants.KEY_ID));
                    dba.open();
            dba.deleteRow(Long.parseLong(id));//remove entry from database according to rowID
            DATA.remove(info.position); //remove entry from arrayadapter, will remove entry from listview
            adapter.notifyDataSetChanged();
            c.close();
benbeel
  • 1,532
  • 4
  • 24
  • 38