片段无法转换为上下文?

Fragment cannot be converted to context?

我正在用 Recyclerview + Cardview 和 GridLayout 开发一个 Android 应用程序,我得到了标题中的错误。

Reminders.java(片段Class)

    public class Reminders extends Fragment {

        private OnFragmentInteractionListener mListener;
        List<reminder> lstreminder;

        public Reminders() {

        }

        public static Reminders newInstance(String param1, String param2) {
            Reminders fragment = new Reminders();
            Bundle args = new Bundle();
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (getArguments() != null) {
            }
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {


                lstreminder = new ArrayList<>();
                lstreminder.add(new reminder("Alarms",R.drawable.alarms));
                lstreminder.add(new reminder("Birthdays",R.drawable.birthdays));
                lstreminder.add(new reminder("Anniversaries",R.drawable.anniversaries));
                lstreminder.add(new reminder("Buy Gifts",R.drawable.buygift));
                lstreminder.add(new reminder("Bill Payments",R.drawable.billpayments));
                lstreminder.add(new reminder("Call / SMS",R.drawable.call));
                lstreminder.add(new reminder("Recharges",R.drawable.recharges));
                lstreminder.add(new reminder("Meetings",R.drawable.meetings));
                lstreminder.add(new reminder("Insurance",R.drawable.insurances));
                lstreminder.add(new reminder("Exams",R.drawable.exams));
                lstreminder.add(new reminder("Holidays",R.drawable.holidays));
                lstreminder.add(new reminder("Medical",R.drawable.medical));
                lstreminder.add(new reminder("Purchase",R.drawable.purchase));
                lstreminder.add(new reminder("Travel",R.drawable.travel));
                lstreminder.add(new reminder("Tv Shows",R.drawable.shows));

            RecyclerView recyclerView = (RecyclerView) getView().findViewById(R.id.mRecyclerView_id);
            RecyclerviewAdapter recyclerviewAdapter = new RecyclerviewAdapter(this,lstreminder);
            recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3));
            recyclerView.setAdapter(recyclerviewAdapter);

            return inflater.inflate(R.layout.fragment_reminders, container, false);
        }

        public void onButtonPressed(Uri uri) {
            if (mListener != null) {
                mListener.onFragmentInteraction(uri);
            }
        }

        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            if (context instanceof OnFragmentInteractionListener) {
                mListener = (OnFragmentInteractionListener) context;
            } else {
                throw new RuntimeException(context.toString()
                        + " must implement OnFragmentInteractionListener");
            }
        }

        @Override
        public void onDetach() {
            super.onDetach();
            mListener = null;
        }

        public interface OnFragmentInteractionListener {
            void onFragmentInteraction(Uri uri);
        }
    }

RecyclerviewAdapter.java

public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.MyViewHolder>{

    private Context mContext;
    private List<reminder> mData;

    public RecyclerviewAdapter(Reminders mContext, List<reminder> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        view = inflater.inflate(R.layout.cardview,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.mTextView.setText(mData.get(position).getmTitle());
        holder.mImageView.setImageResource(mData.get(position).getmImages());
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{

        TextView mTextView;
        ImageView mImageView;

        public MyViewHolder(View itemView) {
            super(itemView);
            mTextView =(TextView)itemView.findViewById(R.id.mReminder_Text_Id);
            mImageView =(ImageView)itemView.findViewById(R.id.mReminder_Image_Id);


        }
    }
}

在上面的代码中,我在这些行上得到了错误

  public RecyclerviewAdapter(Reminders mContext, List<reminder> mData) {
            this.mContext = mContext;
            this.mData = mData;
        }

错误:

error: incompatible types: Reminders cannot be converted to Context 

谁能帮我解决这个问题。

您可以将 getContext() 作为参数发送,该参数可从您的 Fragment 访问。

片段未扩展 Context。您可以使用 getActivity();

将片段的 activity 作为上下文传递

但是您可以在 onCreateViewHolder(ViewGroup parent, int viewType_ 中调用 parent.getContext() 并从适配器中删除上下文。

@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);
}