Fragment cannot be converted to context?

后端 未结 2 689
挽巷
挽巷 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);
    }
    

提交回复
热议问题