为另一个类中的活动创建AletDialog [语境] [重复]

问题描述 投票:0回答:3

这个问题在这里已有答案:

MainActivity [A]

    public class ViewActivity extends AppCompatActivity implements
        FilterDialogFragment.FilterListener,
        AffichageAdapter.OnAffichageSelectedListener {

   public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_view);
           ButterKnife.bind(this);

类[B]:当我在ViewActivity中单击它的节目时,我想创建一个AlertDialogue

public class AffichageAdapter extends FirestoreAdapter<AffichageAdapter.ViewHolder> {

AlertDialog.Builder Alert = new AlertDialog.Builder(What i Put here ! );
android android-context
3个回答
0
投票

AffichageAdapter类中,您可以声明一个这样的字段:

private Context mContext;

public AffichageAdapter(Context context){
      this.mContext = context;//get context by constructor
}

ViewActivity

AffichageAdapter adapter = new AffichageAdapter(this);//"this" means ViewActivity, its a context.
xxx.setAdapter(adapter); // in some place.

然后:

AlertDialog.Builder Alert = new AlertDialog.Builder(mContext);

1
投票

YourClassName.this或getContext()


0
投票

如果您尝试在MainActivity A中显示来自其他类的警报。您应该将MainActivity中的getContext()参数传递给该B类的show alert函数。

© www.soinside.com 2019 - 2024. All rights reserved.