在FirebaseRecycleadapter中连接Firebase中的两个实体

时间:2018-11-17 00:03:49

标签: android firebase firebase-realtime-database

所以我有一个已经在工作的Cardview,这里是代码

  @Override
public void onStart() {
    super.onStart();
FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
        new FirebaseRecyclerAdapter<Model, ViewHolder>(Model.class,R.layout.row,ViewHolder.class,mref)
        {

            @Override
            protected void populateViewHolder(ViewHolder viewHolder, Model model, int position) {
                String userid = model.getUserid().toString();


                viewHolder.setDetails(getActivity().getApplicationContext(),model.getPetname(),"Reason:" + model.getReason(),model.getPetpicture(),"Breed:" + model.getBreed(),
                       "Age:  " + model.getAge(),"Size: " +  model.getPetsize() +"yrs old","Color:" + model.getPetcolor(),model.getUserid(),model.datepost);
            }
        };

    recyclerView.setAdapter(firebaseRecyclerAdapter);
}

那个model.getUserid()获取发布它显示firebase给定的UID的用户的用户ID。现在我想要的是将该UID转换为我从其他实体用户获得的名称 enter image description here enter image description here

因此,简而言之,我想填充Cardview,但我想将其转换为名称,而不是Userid,谢谢您的帮助Godbless

1 个答案:

答案 0 :(得分:0)

您可以做的是,在检索到该userId之后,您可以使用值事件监听器来获取名称,如下所示
                                                                             DatabaseReference userRef = FirebaseDatabase.getInstance()。getReference(“ User”);

    String userId = "the_user_id";

    userRef.child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists())
            {
                String name = dataSnapshot.getValue(User.class).getName();

            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });`