我从item.getItemid()获得“false”值。如果点击特定的popUpMenu,我想得到id的位置。
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listOfPhoneNumber));
listView.setLongClickable(true);
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
Toast.makeText(ViewNumber.this,listView.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
PopupMenu popupMenu = new PopupMenu(ViewNumber.this, view);
getMenuInflater().inflate(R.menu.popupmenu_viewnumber, popupMenu.getMenu());
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getApplicationContext(), item.getItemId(), Toast.LENGTH_SHORT).show();
return false;
}
});
return false;
}
});
答案 0 :(得分:3)
您正在获取菜单项的ID,这是应该如何工作的。
如果您想检查是否点击了某个特定项目,请与您在R.menu.popupmenu_viewnumber
中声明的菜单项ID进行比较,例如:
@Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.YourMenuIdHere) {
//Handle item here
}
return false;
}