如何设置不同的弹出菜单特定的听众? ($ C $里面三) [英] How to set specific listeners for varying popup menus ? (code inside)

查看:165
本文介绍了如何设置不同的弹出菜单特定的听众? ($ C $里面三)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作而在其活动中的一个过滤器按钮与出现每次你点击它弹出菜单的应用程序。特殊的是,这个弹出式菜单中并不总是一样的,它可以根据用户什么确实在previous活动有所不同。

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.

到目前为止,我有codeD,它允许应用程序显示在不同情况下不同的弹出菜单(它运作良好)的一部分。现在,我坚持尝试添加个人监听任何这些情况。结果
这里是我的code。与一些解释:

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

我想现在要做的是为每个听者的分配不同的事件。这就是为什么我定义listener_1,listener_2等。(现在他们都强调了与此注释listener_1不能被解析为一个变量 - 我告诉你,是因为我有点生气与变量,是不是有监听器应该比可变的东西?我们可以初始化一个监听器?),但我不知道如何在这个这类code,这是项点击分配方法的传统例如集成(发现Android开发者):

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;

} }  

感谢您的时间!

推荐答案

我居然找到了一个办法做到这一点,如果它可以帮助别人有一天,我出版我的code。它完美,并允许指定不同的弹出菜单和不同的事件给听者一个只有初始按钮。结果
就我而言,case11符合我的第一个过滤器模式(餐厅例如,每一个项目是一个不同类型的餐馆),cass12另一个过滤模式(夜生活前,与项目不同的活动,等等!)。

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;
}

}

这篇关于如何设置不同的弹出菜单特定的听众? ($ C $里面三)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆