2

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

1

You can pass a context/activity reference to your adapter.

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

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

@Override
public MyAdapter.adapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(mContext).inflate(R.layout.rec_list, parent, false);
    MyAdapter.adapterHolderholder = new MyAdapter.adapterHolder(v);

    return holder;
}.....

But if you use DialogFragment, your activity must be an AppCompatActivity, then the mContext can be converted to an AppCompatActivity.

cy198706
  • 569
  • 7
  • 15
  • Works perfectly thank you very much! It also works when I do it from a DialogFragment, my activity is a AppCompatActivity but how can i convert the mContext to an AppCompatActivity? Anyway it works fine! – Jim Michael Nowell Apr 07 '18 at 03:33
  • 1
    Just Force-Cast in Java. Like ((AppCompatActivity)mContext).xxxx – cy198706 Apr 07 '18 at 03:50
  • It works perfect, thanks for your help! I made myself a mess to try to solve it – Jim Michael Nowell Apr 07 '18 at 04:12
  • @cy198706 i tried your solution but i got TransactionTooLargeException, how can i solve this? – MNFS Aug 14 '19 at 08:16
  • @MNFS Basiclly, when this Exception occurs, it means you are passing a too-large thing to your adapter. Try to reduce the size of your activity. Refer to this link for more help. https://stackoverflow.com/questions/11451393/what-to-do-on-transactiontoolargeexception – cy198706 Aug 21 '19 at 02:41