Display a DialogFragment from an Adapter RecyclerView

1

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!

    
asked by Jim Michael Nowell 07.04.2018 в 01:45
source

1 answer

0

The solution was simpler, simply pass the Context of the Activity or the DialogFragment something like this:

 public class MiAdapter extends RecyclerView.Adapter<MiAdapter.adapterHolder> {
Context mContext;
List<list> list;

public MiAdapter(Context context, List<list> list) {
    this.list = list;
    mContext = context;
}

to be from a DialogFragment to convert the Context to appCompatActivity.

    
answered by 07.04.2018 в 06:45