从ListView中获取PopUpMenu中的“false”值item.getItemId()

时间:2014-01-27 12:49:06

标签: android listview popupmenu

我从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;
    }
});

1 个答案:

答案 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;
}