片段不能转换为上下文



我正在开发一个带有recyclerview cardView的gridlayout的Android应用程序,我在标题中遇到了错误。

sminders.java(fragment类(

    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 

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

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

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

但是,您只能在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);
}

您只需将getContext()发送为可以从片段访问的参数。

编辑:因此,要澄清:

BiometricManager biometricManager = BiometricManager.from(getContext());

BiometricManager biometricManager = BiometricManager.from(getParentFragment().getContext());

相关内容

  • 没有找到相关文章

最新更新