Fragment cannot be converted to context?

后端 未结 2 633
挽巷
挽巷 2021-01-29 07:16

I am developing an Android app with Recyclerview + Cardview with GridLayout in this I got the error as in the title.

Reminders.java (Fragment Class)

             


        
相关标签:
2条回答
  • 2021-01-29 08:00

    Fragment does not extend Context. You could pass the fragment's activity as context using getActivity();

    However you can just call parent.getContext() in onCreateViewHolder(ViewGroup parent, int viewType_ and remove context from the adapter.

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
        View view;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        view = inflater.inflate(R.layout.cardview,parent,false);
        return new MyViewHolder(view);
    }
    
    0 讨论(0)
  • 2021-01-29 08:17

    You can just send getContext() as a parameter which is accessible from your Fragment.

    0 讨论(0)
提交回复
热议问题