如何在Android中使用带有动态引用的FirebaseRecyclerAdapter? [英] How to use a FirebaseRecyclerAdapter with a dynamic reference in Android?

查看:131
本文介绍了如何在Android中使用带有动态引用的FirebaseRecyclerAdapter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在Firebase中的项列表上使用 FirebaseRecyclerAdapter 。我也知道如何使用索引从多个位置获取Firebase数据。但我不知道如何将它与FirebaseRecyclerAdapter结合在一起。



我有一个简单的数据结构,用户可以成为朋友:

 users:{
user1:{
name:name,
friends:{
user2:true
}
}
user2:{
name:name,
friends:{
user1:true
}
}
}

我为用户名使用唯一的ID。在这个简单的例子中,用户1和用户2是朋友。我想要做的是使用FirebaseRecyclerAdapter获取所有名称(以及更多)的列表。通常情况下,我会通过获取朋友下的用户标识,然后使用这些标识导航到他们的名字来获取列表。在Java中,它看起来像这样:

  Firebase ref = new Firebase(< reference> 
ref.child(users / user1 / friends)。addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot snapshot){$ b $ String userId = snapshot .getKey();
ref.child(users /+ userId +/name\").addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot snapshot) {
System.out.println(snapshot.getValue());
}
@Override
public void onCancelled(FirebaseError firebaseError){
//在这里做某事
}
});
}
});但是,当使用FirebaseRecyclerAdapter时,您只能定义一个URL。如果您使用的是FirebaseRecyclerAdapter,则只能定义一个URL。这个适配器会根据这个地方的项目列出一个列表。


有没有办法调整适配器以便从多个地方读取数据上面的例子呢?
或者有没有办法使一个动态参考变量只指向也存储在当前用户朋友列表中的用户ID?我正在考虑使用equals()方法,但我无法弄清楚。



另一种方法是将用户名也存储在friends列表中。但是这不会改善数据结构。我不得不在每个地方改变两个地方的名字(也许以后会有照片),这会引入更多的冗余。



有没有人有解决这个问题的方法?我无法在任何地方找到答案,但它似乎是一种常见的模式。除了使其工作,我该如何有效地做到这一点?

解决方案

您需要加入数据附加 addListenerForSingleValueEvent()

  FirebaseRecyclerViewAdapter< Boolean,ItemViewHolder> adapter = new FirebaseRecyclerViewAdapter< Boolean,ItemViewHolder>(
Boolean.class,android.R.layout.two_line_list_item,ItemViewHolder.class,friendsRef){
protected void populateViewHolder(final ItemViewHolder viewHolder,Boolean model,int position ){
String key = this.getRef(position).getKey();
ref.child(users)。child(key).addListenerForSingleValueEvent(new ValueEventListener(){
public void onDataChange(DataSnapshot dataSnapshot){
String name = dataSnapshot.child(name ).getValue(String.class);
((TextView)viewHolder.itemView.findViewById(android.R.id.text1)).setText(name);
}

public void onCancelled(FirebaseError firebaseError){}
});
}
};

我已经添加了 to to 向我的样本回购展示这个。



使它工作后,我意识到我已经在将FirebaseRecyclerViewAdapter耦合到布尔/字符串Map.Entry ,所以我会将您的问题标记为重复。


I know how to use a FirebaseRecyclerAdapter on a list of items in Firebase. I also know how to get Firebase data from multiple locations using indices. But I do not know how to combine this with a FirebaseRecyclerAdapter.

I have a simple data structure where users can be friends:

"users": {
  "user1": {
    "name": "name",
    "friends": {
      "user2": "true"
      }
    }
  "user2": {
    "name": "name",
    "friends": {
      "user1": "true"
      }
    }
  }

I use unique ids for the usernames. In this simple example, user 1 and user 2 are friends. What I want to do, is get a list of all the names (and later maybe more) using a FirebaseRecyclerAdapter. Normally, I would get a list by obtaining the user id's under "friends" and then using those id's to navigate to their names. In Java, it would look like this:

Firebase ref = new Firebase("<reference>");
ref.child("users/user1/friends").addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot snapshot) {
    String userId = snapshot.getKey();
    ref.child("users/" + userId + "/name").addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot snapshot) {
        System.out.println(snapshot.getValue());
      }
      @Override
      public void onCancelled(FirebaseError firebaseError) {
        // Do something here
      }
    });
  }
});

But when using a FirebaseRecyclerAdapter, you only get to define one URL. The adapter will make a list based on the items in that single place.

Is there a way to adjust the adapter so that it reads data from multiple places, the same way that the example above does? Or is there a way to make a dynamic ref variable that points only to the user id's that are also stored in the current user "friends" list? I was thinking about using the equals() method, but I cannot figure out how exactly.

Another way would be storing the user names also in the "friends" list. But that would not improve the data structure. I would have to change the names in two places every time (and maybe photo's later on) and it would introduce more redundancy.

Does anybody have a solution for this problem? I could not find an answer anywhere but it seems like a common pattern. And apart from just making it work, how do I do it efficiently?

解决方案

You'll need to join the data, by attaching a addListenerForSingleValueEvent().

FirebaseRecyclerViewAdapter<Boolean, ItemViewHolder> adapter = new FirebaseRecyclerViewAdapter<Boolean, ItemViewHolder>(
    Boolean.class, android.R.layout.two_line_list_item, ItemViewHolder.class, friendsRef){
    protected void populateViewHolder(final ItemViewHolder viewHolder, Boolean model, int position) {
        String key = this.getRef(position).getKey();
        ref.child("users").child(key).addListenerForSingleValueEvent(new ValueEventListener() {
            public void onDataChange(DataSnapshot dataSnapshot) {
                String name = dataSnapshot.child("name").getValue(String.class);
                ((TextView)viewHolder.itemView.findViewById(android.R.id.text1)).setText(name);
            }

            public void onCancelled(FirebaseError firebaseError) { }
        });
    }
};

I've added an activity Activity36235919 to demonstrate this to my sample repo.

After making it work I realized that I'd answered this before in Coupling FirebaseRecyclerViewAdapter to a Boolean/String Map.Entry, so I'll mark your question as a duplicate.

这篇关于如何在Android中使用带有动态引用的FirebaseRecyclerAdapter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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