开发者

Geting value from ContextMenu item (database involved)

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜