[英]click on a button, a popup menu will start, and then I click on a popup menu item then selected item will show on buttontext in android studio
[英]Getting the selected item's position in Android popup menu class
我想将一些数据存储到弹出菜单中的每个项目中。 所有项目都基于从提要返回的结果在 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();
}
}
你可以使用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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.