英文:
Android Popup Menu + Database and view changing question
问题
我正在尝试在按下按钮时创建一个弹出菜单。
弹出菜单应该根据数据库中的一列值生成,并且当用户点击菜单选项时,它应该调用函数,以检索与数据库中该值关联的所有项目。
背景:这是一个简单的购物清单,按钮应该允许您仅查看特定商店的物品。
我遇到的问题与 "onclick" 处理程序有关。实际上,我不知道该怎么做。
这是我尝试做的事情,它告诉我 "在此范围内已经声明了 item",但如果我移除 "MenuItem" 数据类型以传递 "item",它仍然会说它已经定义。
我有一种感觉,这是因为它正在创建一个带有自己参数的方法,而不是实际将菜单项传递给它。
如果我只是给它一个不同的参数,比如 (MenuItem) clickedItem,它会自动传递菜单项吗?
public void btnDisplayByStore(View v){
PopupMenu puStoreSelect = new PopupMenu(home_screen.this, findViewById(R.id.btnSearchByStore));
DatabaseManager DBM = new DatabaseManager(this);
ArrayList<String> alStores = DBM.getStores();
for(int i = 0; i < alStores.size(); i++) {
MenuItem item = puStoreSelect.getMenu().add(alStores.get(i));
puStoreSelect.setOnMenuItemClickListener((MenuItem item) -> {
populateItemListByStore(item.getTitle().toString());
return true;
});
}
puStoreSelect.getMenuInflater().inflate(alStores.size(), puStoreSelect.getMenu());
}
英文:
I'm trying to create a popup menu when a button is pressed.
The popup menu is to be generated with the values from a column in the database, and when the user clicks on the menu options, it should then call the function to retrieve all of the items that are associated with that value in the database.
Context: It's a simple shopping list, and the button should allow you to bring up ONLY the items at a particular store.
The problem I'm having is with the "onclick" handler. I actually have no idea how to do this.
This is what I tried to do and it tells me "item is already declared in this scope", but if I remove the "MenuItem" datatype to pass "item" it still says it's already defined.
I have a feeling it's because it's creating a method with its own parameter and not actually passing the menu item to it.
Does it automatically pass the menu item if I simply give it a different param such as (MenuItem) clickedItem ?
public void btnDisplayByStore(View v){
PopupMenu puStoreSelect = new PopupMenu(home_screen.this, findViewById(R.id.btnSearchByStore));
DatabaseManager DBM = new DatabaseManager(this);
ArrayList<String> alStores = DBM.getStores();
for(int i = 0; i < alStores.size(); i++) {
MenuItem item = puStoreSelect.getMenu().add(alStores.get(i));
puStoreSelect.setOnMenuItemClickListener((MenuItem item) -> {
populateItemListByStore(item.getTitle().toString());
return true;
});
}
puStoreSelect.getMenuInflater().inflate(alStores.size(), puStoreSelect.getMenu());
}
专注分享java语言的经验与见解,让所有开发者获益!
评论