Pregunta

i am getting "false" value from item.getItemld(). I want to get postion or id if clicked on the particular popUpMenu.

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;
    }
});
¿Fue útil?

Solución

You are getting the id of your menu item, this is how this is supposed to work.

If you want to check whether a particular item was clicked, compare against the menu item id you declared in R.menu.popupmenu_viewnumber, e.g.:

@Override
public boolean onMenuItemClick(MenuItem item) {
    if (item.getItemId() == R.id.YourMenuIdHere) {
        //Handle item here
    }
    return false;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top