Java 获取所选项目';s在Android弹出菜单类中的位置

Java 获取所选项目';s在Android弹出菜单类中的位置,java,android,Java,Android,我想在弹出菜单中的每个项目中存储一些数据。根据提要返回的结果,在for循环中以编程方式对所有项进行膨胀 在下面的示例中,我使用一个HashMapstoredOption存储每个项目的数据,循环索引作为键。但是我需要找到一种方法来获取所选项目在onMenuItemClick中的位置,以便从storedOption检索数据。谁能告诉我怎么做?除了下面的尝试之外,我还尝试了item.getOrder(),但无论菜单中有多少项,它总是返回0 public DynamicPopUpMenu{

我想在弹出菜单中的每个项目中存储一些数据。根据提要返回的结果,在for循环中以编程方式对所有项进行膨胀

在下面的示例中,我使用一个
HashMap
storedOption
存储每个项目的数据,循环索引作为键。但是我需要找到一种方法来获取所选项目在
onMenuItemClick
中的位置,以便从
storedOption
检索数据。谁能告诉我怎么做?除了下面的尝试之外,我还尝试了
item.getOrder()
,但无论菜单中有多少项,它总是返回0

 public DynamicPopUpMenu{

    private Map<String,FeatureList> storedOption = new HashMap();

    public void main(final Context context,List<FeatureList> featureList){

        int count = 0;
        PopupMenu menu = new PopupMenu(context, featureList);
        for(FeatureList f:featureList) {
            MenuItem item = menu.getMenu().add(f.key);
            storedOption.put(count, f);
            count++;
        }

        menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
               AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                int position = info.position;
                new ShowToast(context,Integer.toString(position)); // show position in a toast
                return true;
            }
        });

        menu.show();
    }

  }
公共动态操作菜单{
私有映射storedOption=newhashmap();
公共void main(最终上下文上下文,列表功能列表){
整数计数=0;
PopupMenu=新的PopupMenu(上下文、功能列表);
用于(功能列表f:功能列表){
MenuItem item=menu.getMenu().add(f.key);
存储地址。放置(计数,f);
计数++;
}
menu.setOnMenuItemClickListener(新的弹出菜单.OnMenuItemClickListener(){
公共布尔onMenuItemClick(菜单项){
AdapterView.AdapterContextMenuInfo信息=(AdapterView.AdapterContextMenuInfo)项。getMenuInfo();
int位置=信息位置;
新建ShowToast(context,Integer.toString(position));//在toast中显示位置
返回true;
}
});
menu.show();
}
}

您可以使用
featureList.key
作为
storeOption
的键,它们使用
item.getItemId()
storeOption
获取值

像这样:

 public DynamicPopUpMenu{

    private Map<String,FeatureList> storedOption = new HashMap();

    public static void main(final Context context,List<FeatureList> featureList){

        int count = 0;
        PopupMenu menu = new PopupMenu(context, featureList);
        for(FeatureList f:featureList) {
            MenuItem item = menu.getMenu().add(f.key);
            storedOption.put(f.key, f);
            count++;
        }

        menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
               AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                int id = item.getItemId();
                FeatureList mFeatureList = (FeatureList)storedOption(id)
                new ShowToast(context,Integer.toString(value)); // show position in a toast
                return true;
            }
        });

        menu.show();
    }

  }
公共动态操作菜单{
私有映射storedOption=newhashmap();
公共静态void main(最终上下文上下文,列表功能列表){
整数计数=0;
PopupMenu=新的PopupMenu(上下文、功能列表);
用于(功能列表f:功能列表){
MenuItem item=menu.getMenu().add(f.key);
存储地址。放置(f键,f);
计数++;
}
menu.setOnMenuItemClickListener(新的弹出菜单.OnMenuItemClickListener(){
公共布尔onMenuItemClick(菜单项){
AdapterView.AdapterContextMenuInfo信息=(AdapterView.AdapterContextMenuInfo)项。getMenuInfo();
int id=item.getItemId();
FeatureList MFFeatureList=(FeatureList)存储选项(id)
新建ShowToast(context,Integer.toString(value));//显示toast中的位置
返回true;
}
});
menu.show();
}
}

值在该
showtoos
中是什么?请尝试获取
id=item.getItemId()并在加载项时将ID设置为menuItemPopupmenu@Shark.I我已经更新了帖子