这个问题在这里已有答案:
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 ! );
在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);
YourClassName.this或getContext()
如果您尝试在MainActivity A中显示来自其他类的警报。您应该将MainActivity中的getContext()参数传递给该B类的show alert函数。