How to create a dynamic context menu in android?

拟墨画扇 提交于 2019-12-08 05:40:16

问题


@Override  
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  

        super.onCreateContextMenu(menu, v, menuInfo);  
        menu.setHeaderTitle("Selection Options");  
        menu.add(0, v.getId(), 0, "Remove");  
    }  

I want my menu to say "Remove AAPL"

I would get the string AAPL from my array adapter, but I am not sure how I can access my array adapters index from this method.


回答1:


Example if you are using listviews with custom object:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    MyObject obj = (MyObject) myListView.getItemAtPosition(info.position);

    menu.setHeaderTitle("Selection Options");  
    menu.add(0, v.getId(), 0, "Remove " + obj.name); 
}



回答2:


Cast menuInfo to an AdapterView.AdapterContextMenuInfo object. From there, you can get the position and id of the item in the ListView that was long-tapped.



来源:https://stackoverflow.com/questions/3631967/how-to-create-a-dynamic-context-menu-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!