4

I tried doing this by the book from android dev docs:

// this didn't create a menu, i don't know why
//registerForContextMenu(getListView());

setListAdapter(new ArrayAdapter<Note>(this, R.layout.selectset_listitem) {
    @Override

    protected View getView(...) {
        ... custom layout ...

        // this creates a menu, but...
        registerForContextMenu(convertView);

        return convertView;
    }
}

And the onCreateContextMenu and onContextItemSelected almost exactly as in http://developer.android.com/guide/topics/ui/menus.html#context-menu.

here is how it looks in the docs (and my code):

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenuInfo menuInfo) {
  super.onCreateContextMenu(menu, v, menuInfo);
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.context_menu, menu);
}

but this part always gives me a null info:

public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    ...
}

The only thing that is really unique is that i've got a custom layout for list items (i.e. a couple of text fields and an image). Is there something i need to do to be able to get the index of the list item that the context menu was built for?

leech
  • 8,293
  • 7
  • 62
  • 78
  • You say onCreateContextMenu is "almost" exactly the same, whats the difference? Can you post that method? – Jack Sep 03 '11 at 05:55
  • That method IS exactly as in the docs. – leech Sep 03 '11 at 13:18
  • why did you comment out registerForContextMenu()? It doesn't "create" a menu, it only registers your list view to listen for long touch events, and call onCreateContextMenu. If you dont registerForContextMenu, then most likely onCreateContextMenu will not work. – Jack Sep 03 '11 at 16:12
  • i commented the first line out because it didn't seem to work. No context menu was created on long taps in the list view. The uncommented call actually does produce a menu. – leech Sep 03 '11 at 16:36
  • Ok now what happens when you debug, and step into onContextItemSelected? Also are you using a custom adapter? If so post the code. – Jack Sep 03 '11 at 18:27
  • debug into onContextItemSelected: info is set to null. I updated the question to include my ArrayAdapter. – leech Sep 03 '11 at 22:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3139/discussion-between-leech-and-jack) – leech Sep 04 '11 at 01:10

2 Answers2

5

You need to call the registerForContextMenu() in the activity on the ListView, and not on the view items in the adapter.

biegleux
  • 13,179
  • 11
  • 45
  • 52
theo
  • 66
  • 1
  • 3
4

getMenuInfo() will work on ListAdapter, not on views.

But, You can pass additional data with the tag of the view.

in

getView: vi.setTag(position) activity.registerForContextMenu(vi);

declare in Activity private int id;

onCreateContextMenu: id = (int) v.getTag();

onContextItemSelected:

you can use the id

Umair
  • 6,366
  • 15
  • 42
  • 50
xnagyg
  • 4,784
  • 2
  • 32
  • 24