如何在不获取当前用户的情况下从firebase数据库中检索所有用户?

时间:2017-11-19 06:40:29

标签: android firebase firebase-realtime-database android-recyclerview

我正在使用Recycler视图从firebase中检索所有用户,如下所示

@Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        databaseReference = FirebaseDatabase.getInstance().getReference().child("users");

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, AllUsersViewHolder>
                (AllUsers.class, R.layout.all_users_display_layout, AllUsersViewHolder.class, databaseReference) {
            @Override
            protected void populateViewHolder(AllUsersViewHolder viewHolder, AllUsers model, final int position) {
                viewHolder.setName(model.getName());

                viewHolder.view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        String visit_user_id = getRef(position).getKey();

                        Intent intent = new Intent(getActivity(), UserProfilesActivity.class);
                        intent.putExtra("visit_user_id", visit_user_id);
                        startActivity(intent);
                    }
                });
            }
        };
        all_user_list.setAdapter(firebaseRecyclerAdapter);
    }

    public static class AllUsersViewHolder extends RecyclerView.ViewHolder {

        View view;

        public AllUsersViewHolder(View itemView) {
            super(itemView);
            view = itemView;
        }

        public void setName(String name) {
            TextView all_user_name = (TextView) view.findViewById(R.id.all_user_name);
            all_user_name.setText(name);
        }
    }
  

我如何在没有当前用户的情况下检索用户因为我正在使用   此列表添加朋友方案

和我的数据库结构如下

users
    key
      name:xxxx
    key
      name:yyyy

请帮我解决这些问题

1 个答案:

答案 0 :(得分:0)

这是一个清单:

databaseReference.orderByChild("id").equalTo(null).addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {        

                   String name=dataSnapshot.child("name").getValue().toString();

                    }

                    @Override
                    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                    }

                    @Override
                    public void onChildRemoved(DataSnapshot dataSnapshot) {

                    }

                    @Override
                    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

使用ChildEventListener列表,它将遍历节点names内的users

所以你可以这样做:

users
key
  name:xxxx
  id:true
key
  name:yyyy