创建查询以获取等于当前用户ID的值

时间:2018-10-03 18:15:55

标签: android firebase firebase-realtime-database

我想从键“ group_member”中获取具有当前用户ID的所有匹配值。密钥“ group_member”由用户标识组成。数据库截图:

enter image description here

为此,我创建查询Query query = mGroupDatabase.child("group_member").equalTo(mCurrent_user_id);

,但没有任何结果。 之后,此结果需要显示到RecyclerView中。

mGroupList = (RecyclerView) mMainView.findViewById(R.id.group_list);

        mAuth = FirebaseAuth.getInstance();
        mCurrent_user_id = mAuth.getCurrentUser().getUid();

        mGroupDatabase = FirebaseDatabase.getInstance().getReference().child("Groups");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setReverseLayout(true);
        linearLayoutManager.setStackFromEnd(true);

        mGroupList.setHasFixedSize(true);
        mGroupList.setLayoutManager(linearLayoutManager);

        Query query = mGroupDatabase.child("group_member").equalTo(mCurrent_user_id);
        query.keepSynced(true);

        FirebaseRecyclerOptions<Group> options =
                new FirebaseRecyclerOptions.Builder<Group>()
                        .setQuery(query, Group.class)
                        .build();

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Group, ViewHolder>(options) {
            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                // Create a new instance of the ViewHolder, in this case we are using a custom
                // layout called R.layout.message for each item
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.adapter_users_activity_layout, parent, false);

                return new ViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(final ViewHolder viewHolder, int position, final Group group) {

                Log.d("sdfdfgghhjhj", group.getGroup_member());

@Override
    public void onStart() {
        super.onStart();
        firebaseRecyclerAdapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();

        if (firebaseRecyclerAdapter != null) {
            firebaseRecyclerAdapter.stopListening();
        }
    }

在上面的代码中,Log.d("sdfdfgghhjhj", group.getGroup_member());没有给出任何值。

组:

package com.example.messenger.messenger;

public class Group {
    String name, group_member;

    public Group() {

    }

    public Group(String name, String group_member) {
        this.name = name;
        this.group_member = group_member;
    }

    public String getGroup_member() {
        return group_member;
    }

    public void setGroup_member(String group_member) {
        this.group_member = group_member;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

1 个答案:

答案 0 :(得分:1)

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

Query query = mGroupDatabase.child("group_member").equalTo(mCurrent_user_id);

Query query = mGroupDatabase.orderByChild("group_member").equalTo(mCurrent_user_id);