在另一个 class [CONTEXT] 中为 activity 创建 AletDialog

Create AletDialog for an activity in another class [CONTEXT]

主要活动[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);

Class [B]: 当我点击它在 ViewActivity 中显示时,我想创建一个 AlertDialogue

public class AffichageAdapter extends FirestoreAdapter<AffichageAdapter.ViewHolder> {

AlertDialog.Builder Alert = new AlertDialog.Builder(What i Put here ! );

YourClassName.this 或 getContext()

如果您尝试在 MainActivity A 中显示来自不同 class 的警报。 您应该将 getContext() 参数从 MainActivity 传递给 Class B 的显示警报功能。

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

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