如何通过for循环中的用户ID检索用户详细信息?

时间:2017-05-08 21:23:41

标签: android firebase firebase-realtime-database

我构建了我的firebase数据库,如下所示:

如果我想通过将id作为参数传递来获取用户详细信息,这就是我用SQL构建数据库表的方式。

但这不符合我的预期。

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) {

        }
    });

这里我已经获取了所有任务,现在我只需要一个参数,这是用户详细信息,但我真的不知道如何获得它。我不确定这是不是正确的方法。也许如果我存储用户名而不是用户ID会更容易。下面我将发布我的代码,尝试通过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;
}

2 个答案:

答案 0 :(得分: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();

    }
}

答案 1 :(得分:0)

请参阅此post

您的DatabaseReference是正确的,但您不需要在' dataSnapshot`上使用getChildren()方法。只需删除第二次迭代。您的代码应如下所示:

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);
}

希望它有所帮助。