I have a problem trying to show a Dialogfragment from an Adapter.
Normally I have done this by calling parent.Context
of inflater
, as follows:
public class MiAdapter extends RecyclerView.Adapter<MiAdapter.adapterHolder>{
Context context;
List<lista> lista;
public MiAdapter (List<lista> lista){
this.lista = lista;
}
@Override
public MiAdapter.adapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rec_lista, parent, false);
MiAdapter.adapterHolderholder = new MiAdapter.adapterHolder(v);
context = parent.getContext();
return holder;
}.....
The onBindViewHolder
is where I show the DialogFragment
after a click.
FragmentManager manager= ((AppCompatActivity)context).getSupportFragmentManager();
OtroFragment manager= new ShPerfil();
OtroFragment.show(manager, "Otro");
However, sometimes I get the following error:
java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.support.v7.app.AppCompatActivity
at com.miapp.Adapters.MiAdapter$1.onClick(MiAdapter.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 is in this link.
However, when implementing it, the application dies whenever I try to show DialogFragment
with the following 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.
Thanks friends!