0
public class VisitorAdapterShop extends RecyclerView.Adapter<VisitorAdapterShop.VisitorHolderShop> {

    private Context context;
    public ArrayList<VisitorShopHelper> visShopsList;

    public VisitorAdapterShop(Context context, ArrayList<VisitorShopHelper> visShopsList) {
        this.context = context;
        this.visShopsList = visShopsList;
    }

    @NonNull
    @Override
    public VisitorHolderShop onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //inflate layout
        View view = LayoutInflater.from(context).inflate(R.layout.row_visitor_shop,parent,false);
        return new VisitorHolderShop(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VisitorHolderShop holder, int position) {
        //get data
        VisitorShopHelper visitorShopHelper = visShopsList.get(position);
        String vis_seller_full_name = visitorShopHelper.getVFullname();
        String vis_seller_email = visitorShopHelper.getVEmail();
        String vis_seller_contact = visitorShopHelper.getVContact();
        String vis_seller_address = visitorShopHelper.getVAddress();
        String vis_seller_image = visitorShopHelper.getVimage();
        String uid = visitorShopHelper.getVuid();

        //set data
        holder.tv_vis_sellerName.setText(vis_seller_full_name);
        holder.tv_vis_sellerEmail.setText(vis_seller_email);
        holder.tv_vis_sellerContact.setText(vis_seller_contact);
        holder.tv_vis_sellerAddress.setText(vis_seller_address);

        try {
            Picasso.get().load(vis_seller_image).placeholder(R.drawable.ic_shop_black_24dp).into(holder.visShopImage);
        }

        catch (Exception e) {
            holder.visShopImage.setImageResource(R.drawable.ic_shop_black_24dp);
        }

    }

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


    //view holder
    class VisitorHolderShop extends RecyclerView.ViewHolder {

        private ImageView visShopImage;
        private TextView tv_vis_sellerName, tv_vis_sellerEmail, tv_vis_sellerContact ,tv_vis_sellerAddress;


        public VisitorHolderShop(@NonNull View itemView) {
            super(itemView);

            visShopImage = itemView.findViewById(R.id.tv_vis_shopImage);
            tv_vis_sellerName = itemView.findViewById(R.id.tv_vis_sellerName);
            tv_vis_sellerEmail = itemView.findViewById(R.id.tv_vis_sellerEmail);
            tv_vis_sellerContact = itemView.findViewById(R.id.tv_vis_sellerContact);
            tv_vis_sellerAddress = itemView.findViewById(R.id.tv_vis_sellerAddress);
        }
    }
}
public class VisitorHomepage extends AppCompatActivity {

    ArrayList <VisitorShopHelper> visShopList;
    VisitorAdapterShop visitorAdapterShop;
    RecyclerView visShopRecycler;
    FirebaseAuth fAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_visitor_homepage);

        visShopRecycler = findViewById(R.id.visShopsRecycler);
        fAuth = FirebaseAuth.getInstance();

       loadShop();

    }

    private void loadShop () {
        visShopList = new ArrayList<>();
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("users");
        databaseReference.orderByChild("Type").equalTo("Seller").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                //clear list at start
                visShopList.clear();

                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    VisitorShopHelper visitorShopHelper = ds.getValue(VisitorShopHelper.class);
                    visShopList.add(visitorShopHelper);
                }

                //setup adapter
                visitorAdapterShop = new VisitorAdapterShop(VisitorHomepage.this,visShopList);

                //set adapter to recycler view
                visShopRecycler.setAdapter(visitorAdapterShop);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


    }
}


First paragraph of code is for the adapter whereas the second paragraph of code is for the activity. When I run the code, I get the error message of adapter not attached, skipping layout. The codes shown here are also used in other parts of my application and it worked. Any help regarding this issue would be highly appreciated.

Kato
  • 40,352
  • 6
  • 119
  • 149
Daniel
  • 29
  • 6
  • 1
    Does this answer your question? [recyclerview No adapter attached; skipping layout](https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout) – Sam Chen Dec 14 '20 at 18:32
  • I tried the one with 250 upvotes, apparently it doesn't solve my question – Daniel Dec 14 '20 at 18:54

3 Answers3

0

You need to set the layout manager to the RecyclerView after initializing it:

visShopRecycler = findViewById(R.id.visShopsRecycler);
visShopRecycler.setLayoutManager(new LinearLayoutManager(this));
Zain
  • 37,492
  • 7
  • 60
  • 84
  • Hi @Zain , I tried your suggestion, but the problem still persist.. logcat still says no adapter attached – Daniel Dec 14 '20 at 18:21
  • Hi @Daniel it seems that the question is closed as duplicate, I also can't find mistakes in your code; can you try solutions from [here](https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout) – Zain Dec 14 '20 at 20:24
0

Set the adapter this way in your mainActivity:

VisitorAdapterShop adapter = new VisitorAdapterShop (this, visShopList); //visShopList is the list of data you wanna show in the recycler view
// Attach the adapter to the recyclerview to populate items
myRecyclerView.setAdapter(adapter);
MehranB
  • 1,460
  • 2
  • 7
  • 17
-1

Pay attention that you're creating a new adapter by the grace of firebase events and not in activity's creation time. Call visShopRecycler.setAdapter(visitorAdapterShop) only once in your activity (onCreate).

In other words, move the part of initializing your adapter and setting it to the recycler out of firebase's onDataChange event. Create new adapter with an empty array in onCreate and not inside any firebase event.

When the event has been triggered, change the adapter's array(using something like visitorAdapterShop.setShopList). Also, you may need to call notifyDataSetChanged method to notify the recycler of the changes.

ronginat
  • 1,910
  • 1
  • 12
  • 23