Android Firebase列出来自键的数据 [英] Android Firebase list data from keys

查看:92
本文介绍了Android Firebase列出来自键的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的 user_projectes 是:





我已经开始玩与Firebase的Android和我结束了这个:
$ b $ pre $ mFirebaseDatabaseReference.child(user_projects)。child(mFirebaseUser.getUid ))
.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot snapshot){
for(DataSnapshot projectSnapshot:snapshot.getChildren()){
Log.e(Project,Project Key found:+ projectSnapshot.getKey());
}
}
@Override $ b $ public void onCancelled(DatabaseError firebaseError ){
Log.e(Chat,The read failed:+ firebaseError.getDetails());
}
});

目前我得到的是项目的钥匙。



我有另一个节点, projects ,它存储项目数据。
项目数据存储在我的 projects / $ projectKey 节点中。



我也必须在我的ListView / GridView中填充项目 name photoURL

我怎样才能检索每个项目键的项目数据,并将其添加到我的列表视图?

解决方案

作为@rhari提到,你不得不作出另一个查询....我发现有用的时候,像这样的加入与firebase是使用RxJava ...可以有一些像下面的例子(你可以使用然后在你的适配器中填充ListView / RecyclerView)

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

在这种情况下,您希望您的列表随着项目的添加而动态更新....在这种情况下,您将需要d版本的listner,它具有 onChildAdded / onChildRemoved 等,然后需要从这些项目中查找项目。 p>

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:

mFirebaseDatabaseReference.child("user_projects").child(mFirebaseUser.getUid())
        .addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                for (DataSnapshot projectSnapshot: snapshot.getChildren()) {
                    Log.e("Project","Project Key found: " + projectSnapshot.getKey());
                }
            }
            @Override
            public void onCancelled(DatabaseError firebaseError) {
                Log.e("Chat", "The read failed: " + firebaseError.getDetails());
            }
        });

Currently what I got are keys for the projects.

I have another node, projects, which stores the project data. The projects data are stored in my projects/$projectKey node.

I will also have to populate the project name and photoURL inside my ListView/GridView.

How I can retrieve the project data for each project key I got and add it to my listview?

解决方案

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.

这篇关于Android Firebase列出来自键的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆