开发者

AdapterContextMenuInfo is always null

开发者 https://www.devze.com 2023-04-01 23:04 出处:网络
I tried doing this by the book from android dev docs: // this didn\'t create a menu, i don\'t know why

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 onContextIte开发者_运维百科mSelected 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?


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


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

0

精彩评论

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