I click on Button5 long click. The context menu appears (Rename, Delete).

enter image description here

I choose to rename and call for example the editButton(button) method and change the label on the button. So I can not figure out how to get Button5 in the method public boolean onContextItemSelected(MenuItem item) ?

 public class MainActivity extends Activity { Button[] buttons = new Button[10]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); createButtons(); for (int i = 0; i < buttons.length; i++) { registerForContextMenu(buttons[i]); } } public void createButtons() { LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout); for (int i = 0; i < buttons.length; i++) { buttons[i] = new Button(this); buttons[i].setId(i); buttons[i].setText("Button " + (i + 1)); linearLayout.addView(buttons[i]); } } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.menu, menu); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case R.id.edit: //Todo //Как здСсь ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ доступ ΠΊ "Button5" Π½Π° ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π½Π°ΠΆΠ°Π»ΠΈ????? //edit(button); Π½Π° ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π½Π°ΠΆΠ°Π»ΠΈ Π΄Π»ΠΈΠ½Π½Ρ‹ΠΌ ΠΊΠ»ΠΈΠΊΠΎΠΌ break; case R.id.delete: break; default: return super.onContextItemSelected(item); } return true; } 

}

  • The MenuItem class has a getActionView () method - pavlofff
  • I write View view = item.getActionView () returns null for some reason. Could you show the sample code. - Denis7371
  • Yes, getActionView() returns only the View , previously set by the setActionView() method, it does not fit here. - pavlofff

1 answer 1

You can use the information provided by AdapterContextMenuInfo :

 View view = info.targetView; 

If necessary, you can view the View to a specific type of widget:

  • info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo (); variable info == null; for some reason. Therefore, View view = info.targetView; does not work. - Denis7371
  • View v = findViewById (item.getItemId ()); v == null; also. Therefore does not work - Denis7371
  • @Denis7371 I also cannot guess why your AdapterContextMenuInfo is null, this should not be and from the presented code it is difficult to understand why this is happening. - pavlofff
  • @Denis7371 AdapterContextMenuInfo returns null because the registerForContextMenu() method works only with the ListView . "Single" View (like Button ) it does not handle. Accordingly, you need to display your buttons through a ListView or look for some crutches to work around. - pavlofff
  • The link says: β€œRegister the View class with which the context menu should be associated, calling the registerForContextMenu () method and passing it the View. If the operation uses ListView or GridView and you want each element to provide the same context menu, register all the elements for the context menu, passing the ListView or GridView method to the registerForContextMenu () method. " ....... Nowhere is it written that registerForContextMenu () works only with the ListView list - Denis7371