设置不同的引用FirebaseRecyclerAdapter android

时间:2018-02-25 12:46:42

标签: android firebase firebase-realtime-database recycler-adapter firebaseui

我有这个数据库结构: enter image description here

当时我有以下代码:

final DatabaseReference messagesRef = mFirebaseDatabaseReference.child("users/").child(firebase_user_uid + "/").child("chats/");
    messagesRef.keepSynced(true);

    FirebaseRecyclerOptions<ChatListMediation> options = new FirebaseRecyclerOptions.Builder<ChatListMediation>().setQuery(messagesRef, parser).build();

    mFirebaseAdapter = new FirebaseRecyclerAdapter<ChatListMediation, MessageViewHolder>(options) {

        @Override
        public MessageViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            final int layout = viewType == TYPE_INACTIVE ? R.layout.item_chatlist : R.layout.item_chatlist_selected;
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(layout, viewGroup, false);
            return new MessageViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(final MessageViewHolder viewHolder, int position, final ChatListMediation chatListMediation) {
            viewHolder.chatKey = chatListMediation.getChatkey();

        }
    };  

所以,我们走了。我为以下FirebasedatabseReference设置了FirebaseRecyclerAdapter:users/firebase_user_uid/chats。所以在这里我得到一个Chatkeys列表。但现在我需要进入FirebaseDatabseReference&#34; chats / chat_key&#34;并在那里阅读信息。例如&#34; chattype&#34;或&#34; createdTime&#34; 。起初,我在我想要的不同的东西上有很多addValueEventListener,但我认为这不是最好的最快方式。也许一秒FirebaseReycyclerAdapter?或者我可以在运行时切换FirebaseRecyclerAdapter的FirebaseDatabseRefernce吗?我不知道该解决谁 我希望你理解我的问题,并能帮助我。如果有任何问题,请告知我们 提前谢谢。

1 个答案:

答案 0 :(得分:1)

要解决此问题,请更改以下代码行:

DatabaseReference messagesRef = mFirebaseDatabaseReference.child("users/").child(firebase_user_uid + "/").child("chats/");

DatabaseReference messagesRef = mFirebaseDatabaseReference
    .child("users")
    .child(firebase_user_uid)
    .child("chats");

如您所见,我删除了/,否则您的引用将包含双/。您也可以这样简短地参考:

DatabaseReference messagesRef = mFirebaseDatabaseReference
    .child("users/" + firebase_user_uid + "/chats/");

我还建议你像这样使用Query

Query query = messagesRef.orderByChild("active").equalsTo(true);