【发布时间】:2017-05-05 16:49:35
【问题描述】:
我创建了一个简单的Custom dialog 类,我想在单击RecycleView 中的一行后显示它。
我的对话框类看起来:
public class AddToQueueDialog extends Dialog implements View.OnClickListener {
Activity mActivity;
private TextView textView1;
private TextView textView2;
private Button save;
public AddToQueueDialog(Activity activity){
super(activity);
mActivity = activity;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_to_queue);
textView1 = (TextView) findViewById(R.id.textView5);
textView2 = (TextView) findViewById(R.id.textView6);
save = (Button) findViewById(R.id.button4);
save.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == save.getId()){
Log.d("save", "save");
}
}
}
我想知道如何正确调用 adapter 的 RecycleView 看起来:
(片)
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ViewHolder(Context context, View itemView, List<WashLocation> washLocations) {
super(itemView);
this.context = context;
info = (TextView) itemView.findViewById(R.id.textView);
favorite = (Button) itemView.findViewById(R.id.addToFav);
favorite.setOnClickListener(this);
info.setOnClickListener(this);
this.washLocations = washLocations;
dataBaseHelper = new DataBaseHelper(context);
}
@Override
public void onClick(View v) {
if(v.getId() == info.getId()){
AddToQueueDialog addToQueueDialog = new AddToQueueDialog(MapsActivity.this);
addToQueueDialog.show();
}
在我的 Custom dialog 类中,我需要一个 Activity in arg 作为构造函数,但我不知道应该在 Adapter 类中传递哪个 Activity
【问题讨论】:
-
通过接口回调更好地从片段/活动调用对话框
-
为什么?可以举个例子吗?
-
@bielas 您需要传递当前正在运行的活动的实例 AddToQueueDialog addToQueueDialog = new AddToQueueDialog((Activity)context); addToQueueDialog.show();
-
@user3333848 是的,我知道,但问题是我应该通过哪个 Activity?
标签: android dialog android-recyclerview adapter