How to set specific listeners for varying popup menus ? (code inside)

邮差的信 提交于 2020-01-03 02:54:17

问题


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.

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.
Here is my code with some explanations :

// 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 ...
    }
}  

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):

@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
    case R.id.archive:
        // ....
        return true;
    case R.id.delete:
        // ....
        return true;

} }  

Thanks for your time!


回答1:


I actually found a way to do this, in case it could help someone someday, I publish my code. It works perfectly and allows to assign different popup menus and different events to the listener for one only initial button.
In my case, case11 corresponds to my first filter mode (Restaurants for example, and every item is a different kind of restaurant), cass12 another filter mode (nightlife for ex, with different activities for items, and so on!).

private void showPopupMenu(View v) {

Bundle bundle = getIntent().getExtras();
int filterVariable = bundle.getInt("filterVariable");

switch (filterVariable) {

case 11:
    PopupMenu myPopupMenu11 = new PopupMenu(
            ResultListViewActivity.this, v);
    myPopupMenu11.getMenuInflater().inflate(R.menu.popup_fastfood,
            myPopupMenu11.getMenu());
    myPopupMenu11
            .setOnMenuItemClickListener(new OnMenuItemClickListener() {

                private String filterInterval;
                private String filterTitle;

                @Override
                public boolean onMenuItemClick(
                        android.view.MenuItem item) {
                    switch (item.getItemId()) {
                    case R.id.item11a:
                        // define your events for item11a
                        return true;

                    case R.id.item11b:
                        //....
                        return true;

                    case R.id.item11c:
                        // ...
                        return true;

                    }
                    return false;
                }
            });

    myPopupMenu11.show();
    break;
case 12:
    PopupMenu myPopupMenu12 = new PopupMenu(
            ResultListViewActivity.this, v);
    myPopupMenu12.getMenuInflater().inflate(R.menu.popup_restaurants,
            myPopupMenu12.getMenu());
    myPopupMenu12
            .setOnMenuItemClickListener(new OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(
                        android.view.MenuItem item) {
                    switch (item.getItemId()) {
                    case R.id.item12a:
                        // ....
                        return true;

                    case R.id.item12b:
                        // ....
                        return true;

                    }
                    return false;
                }
            });

    myPopupMenu12.show();
    break;
}

}



回答2:


What about putting extra from the previous activity and receive it in the activity where the pop up show, you can create a global final variable to identify which pop up will be send so basically it'll like this

BaseID class

public class BaseID {
    public static final int POP_UP_1 = 1;
    public static final int POP_UP_2 = 2;
    public static final int POP_UP_3 = 3;
    public static final int POP_UP_4 = 4;
}

then in previous activity before you start the new activity you should put an extra

Intent i = new Intent(previousactivity, popup.class);
switch(condition) {
   case condition1:
   i.putExtra("popUpType",BaseID.POP_UP_1);
   break;
   case condition2:
   i.putExtra("popUpType",BaseID.POP_UP_2);
   break;
   case condition3:
   i.putExtra("popUpType",BaseID.POP_UP_3);
   break;
   case condition4:
   i.putExtra("popUpType",BaseID.POP_UP_4);
   break;
}
startActivity(i);

then in the pop up activity you can get the extra using getIntExtra()

private int popUpType;

.
.
.

popUpType= getIntent().getIntExtra("popUpType", 0);

then you can create a listener and switch the popUpType

private View.onClickListener mOnClickFilter = new View.OnClickListener() {
  @Override
    public void onClick(View v) {
    switch(popUpType) {
         case BaseID.POP_UP_1:
         //show popup 1 here
         break;
         case BaseID.POP_UP_2:
         //show popup 1 here
         break;
         case BaseID.POP_UP_3:
         //show popup 1 here
         break;
         case BaseID.POP_UP_4:
         //show popup 1 here
         break;
    }
   } 
};

then you can add the listener to the button in the pop up activity in the oncreate

mButtonFilter.setOnClickListener(mOnClickFilter);

and for the setting different action in the pop up I think you can use callback(java interface) first you define the interface

callbackPopUpButton interface

public interface callbackPopUpButton {
    public void onClick();
}

then you can implement it in the activity where you want to show pop up like this

private class cbPopUpButton implements callbackPopUpButton {
     @Override
     public void onClick() {
          switch(popUpType) {
             case BaseID.POP_UP_1:
             //action of button in pop up 1
             break;
             case BaseID.POP_UP_2:
             //action of button in pop up 2
             break;
             case BaseID.POP_UP_3:
             //action of button in pop up 3
             break;
             case BaseID.POP_UP_4:
             //action of button in pop up 4
             break;
         }
     }
}

or if you want to switch based on view you can add View v in the onClick()

after declaring the class you can create a new object and pass it to the popupmenu then calling the onclick like this

myPopupMenu1.setCallback(new cbPopUpButton ); //please create new function to pass the callback

the function and setting the callback should be like this (in myPopupMenu1 class)

private callbackPopUpButton cbPopUpButton

public static void setCallback(callbackPopUpButton cbPopUpButton) {
      this.cbPopUpButton = cbPopUpButton;
}

then you can add the action in the onclick listener of the button like this

buttonPopUp.setOnClickListener = new View.onClickListener() {
    @Override
    public void onClick(View v) {
       cbPopUpButton.onClick();
    }
};

this code will invoke the onClick function in the cbPopUpButton class

that's all!

I hope you can understand my answer if you have any question about my answer feel free to ask in the comment :)



来源:https://stackoverflow.com/questions/15517779/how-to-set-specific-listeners-for-varying-popup-menus-code-inside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!