I have a problem when I try to show a DialogFragment from an Adapter.
Normally I've done this calling the parent.Context
of theinflater
as follow:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.adapterHolder>{
Context context;
List<list> list;
public MyAdapter (List<list> list){
this.list= list;
}
@Override
public MyAdapter.adapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rec_list, parent, false);
MyAdapter.adapterHolderholder = new MyAdapter.adapterHolder(v);
context = parent.getContext();
return holder;
}.....
In the onBindViewHolder
is where I show theDialogFragment
after a click.
FragmentManager manager= ((AppCompatActivity)context).getSupportFragmentManager();
Other manager= new Other();
Other.show(manager, "Other");
But sometimes I get this error:
java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.support.v7.app.AppCompatActivity
at com.miapp.Adapters.MyAdapter$1.onClick(MyAdapter.java:54)
at android.view.View.performClick(View.java:6213)
at android.widget.TextView.performClick(TextView.java:11074)
After investigating for a while I found something useful, the answer that is in this link.
But when I implement it, I always get this error:
java.lang.ClassCastException: ccom.miapp.MyContext cannot be cast to android.support.v7.app.AppCompatActivity
Could someone help me solve this problem? I do not know if I can show the DialogFragment in another way or get the Context in another way.