No adapter attached; skipping layout in fragment using firebase database

后端 未结 2 1815
小蘑菇
小蘑菇 2021-01-27 16:31

I have gone through most of the topics related to it and couldn\'t find the proper solution.
I am getting an error \"E/RecyclerView: No adapter attached; skipping layout
相关标签:
2条回答
  • 2021-01-27 17:01

    Use Debugger to check if you are getting data from firebase database? Or try to Init your Adaptor in OnCreateview method and use notifydatasetchanged()

    0 讨论(0)
  • 2021-01-27 17:05

    You have to set Adapter inside onCreateView then when new data arrives simply call adapter to notifyDataSetChaged

     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View v = inflater.inflate(R.layout.fragment_temp, container, false);
            youradapter = new TempAdapter(getContext(),new ArrayList<TempModel>());
            rcv = (RecyclerView) v.findViewById(R.id.recycle_template);          
            rcv.setAdapter();
            rcv.setLayoutManager(new GridLayoutManager(getContext(),3));
            rcv.setHasFixedSize(true);
            fireAdapter();
            return v;
        }
    

    Then inside onDataChange callback

     @Override
     public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
         for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
             TempModel tempModel = postSnapshot.getValue(TempModel.class);
             tempData.add(tempModel);
           }
           TempAdapter  tempAdapter = new TempAdapter(getContext(),tempData);
           yourAdapter.setData(tempData);
            }
    

    Then inside your TempAdapter class create setData function

    public void setData(List<TempModel> list) {
       if (data != null) data.clear();
       if (data == null) data = new ArrayList<>();
       data.addAll(list);
       notifyDataSetChanged();
    
    }
    
    0 讨论(0)
提交回复
热议问题