0

I've already seen several solutions to that but non was strictly enough related to my example.

I'm creating contextmenu from xml file and filling a ListView from database.

Here is my context menu xml file:

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="Revalue" android:id="@+id/expense_revalue"></item>
    <item android:title="Delete" android:id="@+id/expense_delete"></item>

</menu>

And a ListView dataFill method:

private void fillData() {       

        ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map = new HashMap<String, String>();

        Cursor itemCursor = db.fetchAllItemsInExpenses();       

        if (itemCursor.moveToFirst()){
            do{
                String sCategory = itemCursor.getString(itemCursor.getColumnIndex(db.EXPENSE_CATEGORY));
                String sDate = itemCursor.getString(itemCursor.getColumnIndex(db.EXPENSE_DATE));
                String sValue = itemCursor.getString(itemCursor.getColumnIndex(db.EXPENSE_VALUE));

                map = new HashMap<String, String>();
                map.put(db.EXPENSE_CATEGORY, sCategory);
                map.put(db.EXPENSE_DATE, sDate);
                map.put(db.EXPENSE_VALUE, sValue);
                myList.add(map);                

            }while (itemCursor.moveToNext());           
        }

        SimpleAdapter expensesList = new SimpleAdapter(this, myList, R.layout.expenses_list_item, 
                new String[] {db.EXPENSE_CATEGORY, db.EXPENSE_DATE, db.EXPENSE_VALUE}, 
                new int[] {R.id.expense_category, R.id.expense_date, R.id.expense_value});
        setListAdapter(expensesList);
        //list.setAdapter(expensesList);


        //this.setListAdapter(new ArrayAdapter<String>(this, R.layout.expenses_list_category, category));

    }

What i'm trying to do is proper delete method (but I fail, cos I can't get id matching the database so I could delete a record)

I tried something like this but id doesn't match (what is a value I'm geting this way?)

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
    case R.id.expense_revalue:  
        return true;
    case R.id.expense_delete:           
        db.deleteItem(db.TABLE_EXPENSES, info.id);
        fillData();
        return true;
    }       
    return super.onContextItemSelected(item);
}

public boolean deleteItem(String tableName,long rowId) {
    return SQLDb.delete(tableName, KEY_ROWID + "=" + rowId, null)>0;
}

All I need is the id that matches the id in database... I guess

Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157

1 Answers1

0

Step #1: Use a CursorAdapter (perhaps a SimpleCursorAdapter) instead of the SimpleAdapter.

Step #2: There is no step #2 -- your existing info.id logic should then work, assuming the Cursor has your _id column in it

Here is a sample project demonstrating populating a ListView from a database and using a context menu.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I can't use SimpleCursorAdapter (you're right the info.id solution works then). It's because I need to format some data before puting it in ListView and SimpleCursorAdapter passing the data straight from table to ListView :/ – Jacek Kwiecień Sep 05 '11 at 11:44
  • Maybe it works when I include _id to querry of CursorAdapter? Hmm, gonna check it now – Jacek Kwiecień Sep 05 '11 at 11:46
  • @user928611: You can still use `SimpleCursorAdapter`. Either override `bindView()` or use the `ViewBinder`. I haven't tried `_id` with `SimpleAdapter`, so I do not know if it gets passed along. – CommonsWare Sep 05 '11 at 11:48
  • nope, doesn't work with SimpleAdapter. How can I use SimpleCursorAdapter then? – Jacek Kwiecień Sep 05 '11 at 11:57
  • @user928611: "How can I use SimpleCursorAdapter then?" -- as I wrote, either override `bindView()` or use the `ViewBinder`. Frankly, I don't think you need any of that, since you are not doing anything with the data. See the sample project I link to above. – CommonsWare Sep 05 '11 at 12:05
  • I wen't through that sample. It presents the way I was using before. it doesn't let to work on data before passing to SimpleCursorAdapter. i'm looking into bindView now. Standby :) P.S. I'm not doing anything with the data yet, but I'm planning to as soon I find a way to do that :) – Jacek Kwiecień Sep 05 '11 at 13:28
  • http://stackoverflow.com/questions/4776936/modifying-simplecursoradapter-data That explained the ViewBinder to me. I reworked it and it works. Thanks CommonWare – Jacek Kwiecień Sep 05 '11 at 15:03