Atlas Wormhood
Atlas Wormhood

Reputation: 19

Firebase Recyclerview Retrieve

I cant retrieve the files in my recyclerview here is my code

there is no error in the run..

public class RetrieveActivity extends AppCompatActivity {


    RecyclerView rView;
    private GridViewAdapter gridViewAdapter;
    DatabaseReference databaseReference;
    List<uploadPDF> uploadPDFS;

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

    rView = (RecyclerView) findViewById(R.id.recycler_View);

    databaseReference = FirebaseDatabase.getInstance().getReference("uploads");

    uploadPDFS = new ArrayList<>();
    gridViewAdapter = new GridViewAdapter(this, uploadPDFS);

    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
    rView.setLayoutManager(mLayoutManager);
    rView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
    rView.setItemAnimator(new DefaultItemAnimator());
    rView.setAdapter(gridViewAdapter);

    prepareView();


    }


  public void prepareView(){

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){
                   for(DataSnapshot is : dataSnapshot.getChildren()){
                        Map<String, String> map = (Map) is.getValue();
                        String name = map.get("name");
                        String author = map.get("author");
                        String imageUrl = map.get("imageUrl");
                        String url = map.get("url"); 
                        uploadPDF uploadPDF = new uploadPDF(name, author, imageUrl,  url);
                        uploadPDFS.add(uploadPDF);
}


                }

            }

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

            }
        });
        gridViewAdapter.notifyDataSetChanged();
    }

enter image description here

Upvotes: 1

Views: 31

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598648

You're calling notifyDataSetChanged before the data has actually loaded.

In your onCreate you create the adapter, link it to your view, and give it an (initially empty) array to render. At that point Android will do precisely that: render the grid view without any items in it.

Then in prepareView you start loading data, and immediately calls notifyDataSetChanged which rerenders the grid with an empty array, since the data hasn't loaded yet. Then once that data comes back you put it in the array. But since you never tell Android about the new data, it never repaints the view with the new data.

databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        uploadPDFS.clear();
        if(dataSnapshot.exists()){
           for(DataSnapshot is : dataSnapshot.getChildren()){
                Map<String, String> map = (Map) is.getValue();
                String name = map.get("name");
                String author = map.get("author");
                String imageUrl = map.get("imageUrl");
                String url = map.get("url"); 
                uploadPDF uploadPDF = new uploadPDF(name, author, imageUrl,  url);
                uploadPDFS.add(uploadPDF);
           }
        }
        gridViewAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException();
    }
});

I also:

  • Added a call to uploadPDFS.clear(). Otherwise you'd end up with two copies of each PDF if there's any change in the underlying data. Give it a try, and you'll see what I mean.
  • Added throw databaseError.toException() into onCancelled, because ignoring possible error is a bad idea.

Also see:

Upvotes: 1

Related Questions