【问题标题】:How to call a static method in mainActivity from a custom FirebaseListAdapter class?如何从自定义 FirebaseListAdapter 类调用 mainActivity 中的静态方法?
【发布时间】:2015-09-08 10:15:01
【问题描述】:

我想使用 Firebase 从 ChatListAdapter 类的 mainActivity 中弹出一个警报视图。

问题/错误:

com.firebase.androidchat.Main.activity.this 不能被静态上下文引用

ChatListAdapter 中的代码:

public class ChatListAdapter extends FirebaseListAdapter<Chat> {

...

protected void populateView(View view, Chat chat) {
...    
    MainActivity.displayAmountPopup();
}

ChatListAdapter 中的代码:

public static void displayAmountPopup(){

....

    new AlertDialog.Builder(MainActivity.this)
            .setTitle(strTitle)
            .setMessage(strAmountMessage)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Log.d("AlertDialog", "Positive");
                    // Present Acknowledgement View!!!!!!!!!
                    Intent intent = new Intent(MainActivity.this, AcknowledgementActivity.class);
                    startActivity(intent);

                    /*Couldn't work this Error:local variable mContext is accessed from within inner class; needs to be declared final
                    Intent intent = new Intent(mContext, AcknowledgementActivity.class);
                    //startActivity(intent);
                    mContext.startActivity(new Intent(mContext, AcknowledgementActivity.class));*/
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Log.d("AlertDialog", "Negative");
                }
            })
            .show();
}

【问题讨论】:

    标签: android firebase


    【解决方案1】:

    com.firebase.androidchat.Main.activity.this 不能被静态上下文引用

    因为无法从static 方法/块访问MainActivity.this

    要获得显示AlertDialog的上下文,在displayAmountPopup方法中添加一个上下文参数:

    public static void displayAmountPopup(Context mContext){
    ....
        new AlertDialog.Builder(mContext)
        ....
    }
    

    现在从populateView 调用时将上下文传递给displayAmountPopup 方法:

    MainActivity.displayAmountPopup(view.getContext());
    

    【讨论】:

    • Thx @ρяσѕρєя K,如果我的 AlertDialog 中有一个 startActivity 怎么办(在我上面的代码中注释掉)
    • @user1872384: 在setPositiveButton 按钮上单击 AlertDialog 在启动 Activity 之前自动关闭。你想做什么?
    • 我想在按下“确定”按钮后显示确认视图
    • @user1872384:可以使用Intent intent = new Intent(mContext, AcknowledgementActivity.class);mContext.startActivity(intent);
    • @user1872384: 如果mContext onClick 方法中不可访问,则在方法参数中将其声明为final,如:public static void displayAmountPopup(final Context mContext)
    【解决方案2】:

    您可以在对话框中使用上下文 并投射活动以启动 Intent

    Intent intent = new Intent(context, AcknowledgementActivity.class);
    ((Activity) context).startActivity(intent)
    

    来自注释代码

    public static void displayAmountPopup(Context context){
    
    ....
    
        new AlertDialog.Builder(context)
                .setTitle(strTitle)
                .setMessage(strAmountMessage)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog", "Positive");
                        // Present Acknowledgement View
                        //Intent intent = new Intent(context, AcknowledgementActivity.class);
                        //startActivity(intent);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("AlertDialog", "Negative");
                    }
                })
                .show();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      相关资源
      最近更新 更多