我有很多CardViews,我想在每次cardView单击时显示自定义对话框。我创建了一个MyDialog类,它扩展了DialogFragmnent我想使用我的类“ MyDialog”方法getData(int resource)在RecyclerView内部传递布局文件。 ),但是当我调用它时,它显示错误,应用崩溃了。
这是我的对话框类:
public class MyDialog extends DialogFragment {
int resource;
public void getData(int resource)
{
this.resource=resource;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
LayoutInflater inflater=getActivity().getLayoutInflater();
builder.setView(inflater.inflate(resource,null));
return builder.create();
}
}
这是我的onBindViewHolder方法
@Override
public void onBindViewHolder( MyViewHolder holder, final int position) {
Model model=albums.get(position);
holder.category_name.setText(model.getCategory());
holder.items_count.setText(model.getItems());
holder.thumbnail.setImageResource(model.getThumbnail());
Glide.with(context).load(model.getThumbnail());
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n=albums.get(position).getCategory();
myDialog=new MyDialog();
if (n.equals("Burger"))
{
myDialog.getData(R.layout.custom_dialog_layout);
fm=((AppCompatActivity)context).getSupportFragmentManager();
myDialog.show(fm,"my_dialog");
}
if (n.equals("Burger"))
{
myDialog.getData(R.layout.chawomin);
fm=((AppCompatActivity)context).getSupportFragmentManager();
myDialog.show(fm,"my_dialog");
}
}
});
}