Get view id from oncontextitemselected

I have several buttons registered for the context menu

How to find out which button was pressed to display the menu?

below is the pseudo code that I will use. I need to do something related to the button click (I have a few more buttons for the announcement), from where I find out that the context menu is activated from which the button was clicked.

EDITOR: I think I didn’t understand, I wanted to know which button was pressed so that the menu appeared. No menu item selected. In any case, I have a solution that I will add in the near future.

thank

private static final int SEND_AS_TEXT = Menu.FIRST;
private static final int SEND_AS_IMAGE = Menu.FIRST + 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        sendAllBtn = (Button)findViewById(R.id.sendAllBtn);
        sendAllBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        registerForContextMenu(v);
        openContextMenu(v);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        switch(item.getItemId()){
        case SEND_AS_TEXT:
            //do sth related to the button clicked
            break;

        }
        return super.onContextItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(Menu.NONE, SEND_AS_TEXT, SEND_AS_TEXT, "Send As Text");
        menu.add(Menu.NONE, SEND_AS_IMAGE, SEND_AS_IMAGE, "Send As Image");
    }
+5
source share
5 answers

, , getItemId, , . , .

, , , . , :

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Send As..");
    menu.add(Menu.NONE, SEND_AS_TEXT, SEND_AS_TEXT, "Send As Text");
    menu.add(Menu.NONE, SEND_AS_IMAGE, SEND_AS_IMAGE, "Send As Image");
    btnId = v.getId(); //this is where I get the id of my clicked button
}

btnId, , .

+11

, ID . , ListView, , , , /. , / .

"position" MenuInfo.id, .

, / ListView (view.setId(x), x ID/ /. ContextMenu , :

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    int id = info.targetView.getId();

    // now you can refer to your data with the correct ID of yours
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    int id = info.targetView.getId();

    // now you can refer to your data with the correct ID of yours
}
+4

( getItemId(int)), onContextItemSelected:

final AdapterView.AdapterContextMenuInfo info = 
  (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final long datasId = info.id    // get datas id
+1
@Override
public boolean onContextItemSelected(MenuItem item) {
    item.getItemId();
    return super.onContextItemSelected(item);
}
0

...

@Override
public boolean onContextItemSelected(MenuItem item)
 {
    if(item.getItemId()==SEND_AS_TEXT)
    {
        //code for send text
    } 
    else if(item.getItemId()==SEND_AS_IMAGE)
    {
       //code for send image
    }
    return super.onContextItemSelected(item);
}
0

All Articles