Android 如何从自定义FirebaseListAdapter类调用mainActivity中的静态方法?
我想使用Firebase从ChatListAdapter类在mainActivity中弹出一个警报视图 问题/错误:Android 如何从自定义FirebaseListAdapter类调用mainActivity中的静态方法?,android,firebase,Android,Firebase,我想使用Firebase从ChatListAdapter类在mainActivity中弹出一个警报视图 问题/错误: public class ChatListAdapter extends FirebaseListAdapter<Chat> { ... protected void populateView(View view, Chat chat) { ... MainActivity.displayAmountPopup(); } public stati
public class ChatListAdapter extends FirebaseListAdapter<Chat> {
...
protected void populateView(View view, Chat chat) {
...
MainActivity.displayAmountPopup();
}
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。这不能从静态上下文引用
ChatListAdapter中的代码:
public class ChatListAdapter extends FirebaseListAdapter<Chat> {
...
protected void populateView(View view, Chat chat) {
...
MainActivity.displayAmountPopup();
}
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。这不能从静态上下文引用
因为无法访问main活动。此
来自静态
方法/块
要获取显示AlertDialog
的上下文,请向displayAmountPopup
方法添加一个上下文参数:
public static void displayAmountPopup(Context mContext){
....
new AlertDialog.Builder(mContext)
....
}
现在,从populateView
调用时,将上下文传递给displayamountpoop
方法,如下所示:
MainActivity.displayAmountPopup(view.getContext());
您可以在对话框中使用上下文 和演员活动开始意图
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();
}
Thx@ρцσρѕρєK,如果我的AlertDialog中有startActivity(在上面的代码中注释掉了)@user1872384:on
setPositiveButton
按钮单击AlertDialog会在开始活动之前自动取消。您想做什么?单击“确定”按钮后,我想显示确认视图pressed@user1872384:ok useIntent Intent=new Intent(mContext,ackknowledgementactivity.class);mContext.startActivity(意图)
@user1872384:如果在onClick
方法中无法访问mContext
,则在方法参数中将其声明为final,如:public static void displayamountpoup(final Context mContext)