为另一个类[CONTEXT]中的活动创建AletDialog

时间:2018-09-19 03:12:28

标签: android android-context

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

3 个答案:

答案 0 :(得分:1)

YourClassName.this或getContext()

答案 1 :(得分:0)

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

答案 2 :(得分:0)

SELECT *, COUNT(mobile_no) FROM duplicate GROUP BY mobile_no HAVING COUNT(mobile_no) > 1; 类中,您可以这样声明一个字段:

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.