How to get data from a cursor in ContextMenu

I want to get the current cursor entry, not just the identifier, so that I can manipulate the context menu.

I saw this example here which shows you how to get the id:

@Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case DELETE_ID: AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); delete(info.id); return(true); } return(super.onOptionsItemSelected(item)); } 

This is great because it allows me to get the corresponding SQLite database identifier in the context menu with a click, which allows me to write a function to search for. But of course, can I just reuse the current cursor?

I tried to do this:

 Cursor c = (Cursor) this.getListAdapter().getItem((int) info.id); String itemPriority = c.getInt(1); Log.v(TAG, "Current item:" + itemPriority); 

but the cursor line seems to return only the database schema and not the record I am after.

Can someone shed some light.

EDIT: Thanks to @azgolfer, I found a solution. I use the fillData () method to populate the adapter. This is usually declared without variables. I had to override this method with a field variable. The relevant piece of code to make the curstor adapter visible in onContextItemSelected is here:

 private void fillData() { Cursor itemsCursor = mDbHelper.fetchAllItemsFilter(mListId, mStatusFilter); startManagingCursor(itemsCursor); mItemAdaptor = new ItemAdapter(this, itemsCursor); this.setListAdapter(mItemAdaptor); } 
+6
source share
1 answer

You use the CursorAdapter to display list items, right? Then you already have access to the cursor by calling getCursor () in the CursorAdapter itself. To get the correct entry from the cursor, you just need to get the user’s position by pressing the button, then move the cursor to that position and extract your data.

First determine the position of pressing the button:

 AdapterView.AdapterContextMenuInfo info= (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); int itemPosition = info.position; 

Then move the cursor to the desired position and extract the entry:

 Cursor cursor = (Your cursorAdapter).getCursor(); cursor.moveToPosition(itemPosition); String itemPriority = cursor.getInt(1); Log.v(TAG, "Current item:" + itemPriority); 
+13
source

Source: https://habr.com/ru/post/921426/


All Articles