Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set specific listeners for varying popup menus ? (code inside)
    primarykey
    data
    text
    <p>I am working on an app which has in one of its activity a "Filter Button" with a popup menu appearing everytime you click on it. The particular thing is that this popup menu is not always the same, it can vary depending on what the user does in the previous activity. </p> <p>So far, I have coded the part which allows the app to display different popup menu in different cases (it works well). Now, I am stuck trying to add individual listeners for any of these cases.<br> Here is my code with some explanations : </p> <pre><code>// method called when the user clicks on "filter_button" // displays a different popupmenu depending on the value of // filterVariable defined in a previous activity private void showPopupMenu(View v) { Bundle bundle = getIntent().getExtras(); int filterVariable = bundle.getInt("filterVariable"); switch (filterVariable) { // Case 1: inflates Res/Menu/popupmenu case 1: PopupMenu myPopupMenu1 = new PopupMenu(ResultListViewActivity.this, v); myPopupMenu1.getMenuInflater().inflate(R.menu.popupmenu, myPopupMenu1.getMenu()); myPopupMenu1.setOnMenuItemClickListener(listener_1); myPopupMenu1.show(); break; // Case 2: inflates Res/Menu/popumenu2 case 2: PopupMenu myPopupMenu2 = new PopupMenu(ResultListViewActivity.this, v); myPopupMenu2.getMenuInflater().inflate(R.menu.popupmenu2, myPopupMenu2.getMenu()); myPopupMenu2.setOnMenuItemClickListener(listener_2); myPopupMenu2.show(); break; // and so on ... } } </code></pre> <p>What I want to do now is to assign different event for each of the listener. It is why I have defined listener_1, listener_2, etc. (right now they are underlined with this annotation "listener_1 cannot be resolved to a variable" - I tell you this because I am a bit upset with "variable", isn't a listener supposed to be something else than a variable? can we initialize a listener?) but I don't know how to integrate this in this kind of code, which is the traditional example of assigning method to item clicks (found on Android Developer): </p> <pre><code>@Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.archive: // .... return true; case R.id.delete: // .... return true; } } </code></pre> <p>Thanks for your time!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload