0

I have structured my firebase database like this:

And this is the way how i would structure my tables in database with SQL if i want to fetch user details by passing id as parameter.

But this is not working as i expected.

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

            for (final DataSnapshot snapshot : dataSnapshot.getChildren()) {
                final String taskName = snapshot.child("name").getValue(String.class);
                final String assignedUserId = snapshot.child("assignedUserId").getValue(String.class);
                final String categoryId = snapshot.child("categoryId").getValue(String.class);
                final Boolean completed = snapshot.child("completed").getValue(Boolean.class);
                final String priority = snapshot.child("priority").getValue(String.class);

                User user = getUser(assignedUserId);

                ProjectTask projectTask
                        = new ProjectTask(snapshot.getKey(), dataSnapshot.getKey(), taskName, assignedUserId, priority, completed, categoryId, user);
                mProjectTaskList.add(projectTask);
            }

            for (Category category : mCategories) {
                mProjectTasksSection = getTasksWithSection(category);

                if (mProjectTasksSection.size() > 0) {
                    mSectionedRecyclerViewAdapter.addSection(new ProjectTaskListAdapter(R.layout.lst_todo_item_v2, mProjectTasksSection,
                            category, getActivity()));
                }
            }

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
            recyclerProjectTasks.setLayoutManager(linearLayoutManager);
            recyclerProjectTasks.setItemAnimator(new DefaultItemAnimator());
            recyclerProjectTasks.setAdapter(mSectionedRecyclerViewAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Here i have fetched all tasks and now i just need one more parameter and that is user details, but i really don't know how to get it. I'm not really sure if this is the right way. Maybe it would be easier if i store user name instead of user id. Below i will post my code trying to fetch user details by id:

private User getUser(String keyId) {
    DatabaseReference databaseReference = AppController.getInstance().getDatabase()
            .getReference().child("users").child(keyId);

    databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            User user = dataSnapshot.getValue(User.class);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    return null;
}
AL.
  • 36,815
  • 10
  • 142
  • 281
Dusan Dimitrijevic
  • 3,169
  • 6
  • 22
  • 46
  • Is your question about whether or not what you're doing in the last code snippet the right thing to do? If so, at surface level this looks fine with me. Given that your `p-tasks` object stores a reference to your `User` object, resolving the user using another `DatabaseReference` seems appropriate. – Brian May 08 '17 at 21:39
  • Exactly, but i can't return user object in method `getUser()` and if i try to create one global User object instance and initialize it in this method `getUser()` for some reason i'm getting null later when i'm updating UI, but i have test it and user object was not null, but it seems like it wasn't initialized before this next for loop started. – Dusan Dimitrijevic May 08 '17 at 21:44
  • This link is good to match your Question. I think this is helpful to you https://stackoverflow.com/a/45328201/5973946 – viral 9966 Jul 28 '17 at 13:11

2 Answers2

0
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{   
//pass name here that you want to 
//   if(postSnapshot.getKey().equals("name".equalsIgnoreCase(nameStr))) 
   //compare
   if(postSnapshot.getKey().equals("name"))
 {
    final String taskName = snapshot.child("name").getValue(String.class);
    //taskName =postSnapshot.getValue().toString();

    }
}
Atif AbbAsi
  • 5,633
  • 7
  • 26
  • 47
0

Please see this post.

Your DatabaseReference is correct but you don't need to use getChildren() method on the 'dataSnapshot`. Just remove the second iteration. You code should look like this:

public void onDataChange(final DataSnapshot dataSnapshot) {

    final String taskName = snapshot.child("name").getValue(String.class);
    final String assignedUserId = snapshot.child("assignedUserId").getValue(String.class);
    final String categoryId = snapshot.child("categoryId").getValue(String.class);
    final Boolean completed = snapshot.child("completed").getValue(Boolean.class);
    final String priority = snapshot.child("priority").getValue(String.class);

     User user = getUser(assignedUserId);

     ProjectTask projectTask = new ProjectTask(snapshot.getKey(), dataSnapshot.getKey(), taskName, assignedUserId, priority, completed, categoryId, user);
     mProjectTaskList.add(projectTask);
}

Hope it helps.

Community
  • 1
  • 1
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193