简体   繁体   中英

RecyclerView is not showing data from Firestore

recyclerView = findViewById(R.id.companyFeed_RecyclerView_CompanyList);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        ArrayList<Company> myCompanyData = new ArrayList<>();
        myCompanyData.add(new Company("Company marc","trustworthy",null));
        db.getFirestore().collection("companies").addSnapshotListener((value, error) -> {
            assert value !=null;
            for (QueryDocumentSnapshot snapshot : value) {
                Company c = Deserialization.deserializeCompany(snapshot);
                myCompanyData.add(new Company(c.getName(),c.getDescription(),null));
                System.out.println("company: " + myCompanyData.get(1).getName());
            }
        });
        CompanyAdapter myCompanyAdapter = new CompanyAdapter(myCompanyData, CompanyFeedActivity.this);
        recyclerView.setAdapter(myCompanyAdapter);

My RecyclerView only shows the element "Company mark" before the snapshotListener. My database works perfectly fine. System.out.println shows me the right element with the right name.

Is there any better way to access the elements or did I make something wrong?

This is how my database looks like:

Firestore-root
  |
  --- companies (collection)
        |
        --- company 1 (document)
             |
             --- name: Marc
             |
             --- description: test
        |
        --- company 2 (document)
             |
             --- name: Lucas
             |
             --- description: test2

I think the problem is that the listener is triggered after the adapter is created and set. You need to explicitly call myCompanyAdapter.notifyDataSetChanged after the data list is modified.

Please try the below code

 recyclerView = findViewById(R.id.companyFeed_RecyclerView_CompanyList);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        ArrayList<Company> myCompanyData = new ArrayList<>();

        CompanyAdapter myCompanyAdapter = new CompanyAdapter(myCompanyData, CompanyFeedActivity.this);
        recyclerView.setAdapter(myCompanyAdapter);
        myCompanyData.add(new Company("Company marc","trustworthy",null));
        db.getFirestore().collection("companies").addSnapshotListener((value, error) -> {
            assert value !=null;
            for (QueryDocumentSnapshot snapshot : value) {
                Company c = Deserialization.deserializeCompany(snapshot);
                myCompanyData.add(new Company(c.getName(),c.getDescription(),null));
                System.out.println("company: " + myCompanyData.get(1).getName());
            }
            myCompanyAdapter.notifyDataSetChanged()
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM