开发者

How to get the view that initiated the showing of context menu?

开发者 https://www.devze.com 2023-03-12 16:28 出处:网络
I have ListView, I register to listen for context 开发者_如何学Cmenu. After that I got 3 items in the listView A,B and C. Than when I long press on some of them the context menu is show, but how can I

I have ListView, I register to listen for context 开发者_如何学Cmenu. After that I got 3 items in the listView A,B and C. Than when I long press on some of them the context menu is show, but how can I know if that was the A or B or maybe C ? how can I find out which item was pressed ?


you must be using some List to populate the ListView... Then use following:

@Override
public boolean onContextItemSelected(MenuItem item)
{
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
            .getMenuInfo();
    int temp = info.position;


    //temp is the index of your list.. simply use this:
    String item = list.get(temp);

    return true;
}

Hope it helps

UPDATE::

To check which item was pressed and which menu to show, see following:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo)
{
    if (v.getId() == your listview id)
    {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

        //Now simply use info.position to get the string from your list. use
        // if else statements and use menu.add(String) to add menu items....           
    }


Here some examples how to work with ContextMenu, you just need to override onContextItemSelected(MenuItem item)

0

精彩评论

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