0

So inside my for loop I am retrieving values from one path and then in the query I am using the creatorUid from the first for loop to get other strings from another path. I need to find a way to be able to retrieve these strings and add them to the arraylist located in the first for loop but not the nested for loop. If you need clarification on what i am talking about i have comments in the code below:

databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            chatsItemArrayList.clear();

            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                title = "" + ds.child("title").getValue();
                description = "" + ds.child("description").getValue();
                date = "" + ds.child("datecreated").getValue();
                speaks = "" + ds.child("speaks").getValue();
                upvotes = "" + ds.child("upvotes").getValue();
                views = "" + ds.child("views").getValue();
                picture = "" + ds.child("chatpicture").getValue();
                String identifier = "" + ds.child("identifier").getValue();
                creatorUid = "" + ds.child("chatcreatoruid").getValue();

                System.out.println(creatorUid + " THIS IS THE CREATOR UID");


                Query query = FirebaseDatabase.getInstance().getReference().child("Users").orderByKey().equalTo(creatorUid);
                query.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {

                          //HOW DO I GET THESE VALUES FROM HERE:

                            rank = "" + dataSnapshot1.child("rank").getValue();
                            profilePicture = "" + dataSnapshot1.child("image").getValue();
                            username = "" + dataSnapshot1.child("username").getValue();

                            System.out.println("THIS IS THE RANK " + getRankText() + " THE USERNAME " + getUsernameText() + " PROFILEPICTURE " + getProfilePicture());

                        }
                    }

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

                    }
                });

                System.out.println("R1" + getRankText() + " U1" + getUsernameText() + " P1 " + getProfilePicture());

                 // TO HERE:

                chatsItemArrayList.add(new ChatsItem(getProfilePicture(), title, date, speaks, description, getUsernameText(), getRankText(), upvotes, views, picture));

            }

            mAdapter = new ChatsFragmentAdapter(chatsItemArrayList);
            mRecyclerView.setAdapter(mAdapter);
        }

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

        }
    });
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Mr. Octodood
  • 121
  • 8

1 Answers1

0

Data from Firebase is only available inside the onDataChange method, or in code called from there. There is no workaround for this.

If you require an additional database call to load additional data, you should:

  • either hold off adding the item to the list at first, and then only add it in the inner onDataChange
  • or add the incomplete item to the list at first, and then update it in the inner onDataChange

So the simplest fix is to do the first, which looks something like this:

query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
            rank = "" + dataSnapshot1.child("rank").getValue();
            profilePicture = "" + dataSnapshot1.child("image").getValue();
            username = "" + dataSnapshot1.child("username").getValue();

            chatsItemArrayList.add(new ChatsItem(getProfilePicture(), title, date, speaks, description, getUsernameText(), getRankText(), upvotes, views, picture));

        }
    }

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

You'll typically want to also refresh the adapter in that inner onDataChange method by calling something like adapter.notifyDataSetChanged().

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807