如何从自定义 FirebaseListAdapter class 调用 mainActivity 中的静态方法?
How to call a static method in mainActivity from a custom FirebaseListAdapter class?
我想使用 Firebase 在 ChatListAdapter class 的 mainActivity 中弹出一个警报视图。
Problem/Error:
com.firebase.androidchat.Main.activity.this cannot be reference from a static context
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();
}
com.firebase.androidchat.Main.activity.this cannot be reference from a static context
因为无法从 static
method/block.
访问 MainActivity.this
要获取用于显示 AlertDialog
的上下文,请将一个上下文参数添加到 displayAmountPopup
方法:
public static void displayAmountPopup(Context mContext){
....
new AlertDialog.Builder(mContext)
....
}
现在从 populateView
调用时将上下文传递给 displayAmountPopup
方法:
MainActivity.displayAmountPopup(view.getContext());
您可以在对话框中使用上下文
并施放 activity 以启动 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();
}
我想使用 Firebase 在 ChatListAdapter class 的 mainActivity 中弹出一个警报视图。
Problem/Error:
com.firebase.androidchat.Main.activity.this cannot be reference from a static context
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();
}
com.firebase.androidchat.Main.activity.this cannot be reference from a static context
因为无法从 static
method/block.
MainActivity.this
要获取用于显示 AlertDialog
的上下文,请将一个上下文参数添加到 displayAmountPopup
方法:
public static void displayAmountPopup(Context mContext){
....
new AlertDialog.Builder(mContext)
....
}
现在从 populateView
调用时将上下文传递给 displayAmountPopup
方法:
MainActivity.displayAmountPopup(view.getContext());
您可以在对话框中使用上下文 并施放 activity 以启动 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();
}