如何在firebaserecycleradapter上从firebase数据库中获取数据

时间:2018-05-11 13:17:31

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

适配器

firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<AllUsers, UsersViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull AllUsers model) {
            holder.setName(model.getName());
            holder.setStatus(model.getStatus());

            final String user_id = getRef(position).getKey();
            final String user_name = getRef(position).

            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), Chat.class);
                    intent.putExtra("Recievers_Id", user_id);
                    intent.putExtra("Recievers_Name", user_name);

                    startActivity(intent);
                }
            });

        }

        @Override
        public UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.all_users_layout, parent, false);

            return new UsersViewHolder(view);
        }
    };
    mUsersList.setAdapter(firebaseRecyclerAdapter);

    return rootView;
}

我知道.getkey();返回父数据...但在父母内部我有一个孩子,这是“名字”,我想要获取...我如何实现这一目标

数据库截图 - https://ibb.co/mBKZLd

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");

reference.addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
public void onDataChange(DataSnapshot dataSnapshot) {
   for(DataSnapshot datas: dataSnapshot.getChildren()){
       String keys=datas.getKey();
       String name=datas.child("Name").getValue().toString();
      }
 }
  @Override
public void onCancelled(DatabaseError databaseError) {
    }
 });

第一张快照位于Users,然后迭代并获取密钥和名称