简体   繁体   中英

Trying to retrieve data from another firebase database

I am trying to retrieve data from another firebase database but I get nothing to show. I don't get any errors and my app doesn't crash which is a good thing, but it doesn't show any data and there is data in the database.

Can someone please help me resolve this issue?

recyclerView.setLayoutManager(layoutManager);
myUploads = new ArrayList<Model_Information>();
aAdapter = new Adapter1(getContext(), myUploads);
recyclerView.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();

DatabaseReference databaseReference = FirebaseDatabase.getInstance("https://boot0-43081.firebaseio.com/").getReference("Posts");

if (InternetConnection.checkConnection(getContext())) {
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
                    Model_Information upload=postsnapshot.getValue(Model_Information.class);

                    myUploads.add(upload);
           
                    recyclerView.invalidate();
                }
                linearLayoutWithoutItems.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);

             aAdapter.notifyDataSetChanged();

            }else{
                linearLayoutWithoutItems.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);

            }

Json

{
  "Posts" : {
    "37trzrENv0WGEgHWTPvKZ08wWcY2" : {
      "-MT1gIzSVIG4cF_ipquI" : {
        "created" : "2-09-2021",
        "headline" : "Danny Dinner",
        "id" : "-MT1gIzSVIG4cF_ipquI",
        "mImageUrl" : "https://firebasestorage.googleapis.com/v0/b/boot0-43081.appspot.com/o/37trzrENv0WGEgHWTPvKZ08wWcY2%2F1612805391640.jpg?alt=media&token=9e819b43-507d-4c07-91cb-c006e16a8def",
        "main_id" : "-MT1gIzSVIG4cF_ipquI",
        "time" : "1:29 AM",
        "timestamp" : 1612805393601,
        "views" : 0,
        "websiteurl" : "www.danny.com"
      }
    }
  },

From the looks of it your Posts node has a key with the UID of a user under it, and then under there is a list of the posts for that user.

Since your code reads the entire Posts node, it need to handle both level of nesting here: users and posts. So you need two nested loops:

DatabaseReference databaseReference = FirebaseDatabase.getInstance("https://boot0-43081.firebaseio.com/").getReference("Posts");

if (InternetConnection.checkConnection(getContext())) {
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
                    for (DataSnapshot postsnapshot : userSnapshot.getChildren()) {
                        Model_Information upload=postsnapshot.getValue(Model_Information.class);

                        myUploads.add(upload);           
                        recyclerView.invalidate();
                    }
                }
                linearLayoutWithoutItems.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);

             aAdapter.notifyDataSetChanged();
            }else{
                linearLayoutWithoutItems.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
            }

Or if you only want to read the posts for the current user, that'd be:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference databaseReference = FirebaseDatabase.getInstance("https://boot0-43081.firebaseio.com/")
    .getReference("Posts").child(uid);

And then you can leave the onDataChange unmodified.

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