Skip to content Skip to sidebar Skip to footer

Android Firebase List Data From Keys

I have a node that stores a list of project keys for each user. The structure of my user_projectes is: I have started to play with Firebase for Android and I ended up with this: m

Solution 1:

Unfortunately, as @rhari mentions, you do have to make another query....what I've found useful when doing "joins' like this with firebase is to use RxJava...could have something like following for example (which you could use then in your adapter to populate ListView/RecyclerView)

public Observable<Project> getProjects(String userId) {
    return getProjectKeys(userId).flatMap(projectKey -> getProject(projectKey));
}

Where this falls down is where you want your list to dynamically update as projects are added....in that case you'll need version of listner that has onChildAdded/onChildRemoved etc and will need then to do lookup of project from those.

Solution 2:

Just query the database again

mFirebaseDatabaseReference.child("user_projects").child(mFirebaseUser.getUid())
    .addListenerForSingleValueEvent(newValueEventListener() {
        @OverridepublicvoidonDataChange(DataSnapshot snapshot) {
            for (DataSnapshotprojectSnapshot: snapshot.getChildren()) {
                String key = projectSnapshot.getKey();                         
                mFirebaseDatabaseReference.child("projects").child(key)
               .addListenerForSingleValueEvent(newValueEventListener() {
                   @OverridepublicvoidonDataChange(DataSnapshot snapshot1) {
                       Project p = snapshot1.getValue();
                   }
               }
        }
        @OverridepublicvoidonCancelled(DatabaseError firebaseError) {
            Log.e("Chat", "The read failed: " + firebaseError.getDetails());
        }
    });

Solution 3:

You have to create a model for your project entity then you can use Project mProject = projectSnapshot.getValue(Project.class);

Post a Comment for "Android Firebase List Data From Keys"