片段无法转换为上下文?

时间:2018-04-26 14:55:52

标签: android android-recyclerview android-cardview realmrecyclerviewadapter

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

Reminders.java(片段类)

    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 

任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

您只需发送mutate作为可从您的片段访问的参数。

答案 1 :(得分:0)

片段不会延伸editable: true。您可以使用Context;

将片段的活动作为上下文传递

但是,您只需在getActivity()中调用parent.getContext()并从适配器中删除上下文。

onCreateViewHolder(ViewGroup parent, int viewType_