如何通过使用另一个表从Firebase表获取数据

时间:2020-04-24 09:16:59

标签: android firebase firebase-realtime-database

因此,我尝试先将我的订单列为用户,然后再单击时将其带到可以看到所有项目的地方。我已经将订单ID设置为用户uid,现在我正在尝试获取uid并从中获取用户数据。我在开始时就将mykey定义为public,但是我认为我在循环内部使用它,因此无法在外部访问它。请帮忙

enter image description here

我正在尝试的代码

firebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Order");
    firebaseDatabase.keepSynced(true);


    firebaseDatabase2 = FirebaseDatabase.getInstance().getReference().child("Users");
    firebaseDatabase.keepSynced(true);



    firebaseDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                mykey = snapshot.getKey();


            }



        }

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

        }
    });


@Override
protected void onStart() {
    super.onStart();





    FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users>().setQuery(firebaseDatabase2.child(mykey), Users.class).build();


    FirebaseRecyclerAdapter<Users, Orders.UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, Orders.UsersViewHolder>(options)
    {

        @Override
        public Orders.UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_layout,parent,false);

            return new Orders.UsersViewHolder(view);

        }

        @Override
        protected void onBindViewHolder(Orders.UsersViewHolder holder, int position, Users model) {

            holder.setName(model.getName());
            holder.setStatus(model.getMobile());






        }

    };

    mylist.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.startListening();


}

public class UsersViewHolder extends RecyclerView.ViewHolder {

    View mview;


    public UsersViewHolder(@NonNull View itemView) {
        super(itemView);

        mview = itemView;
    }


    public void setName(String name) {

        TextView userNameView = mview.findViewById(R.id.username);
        userNameView.setText(name);

    }


    public void setStatus(String status) {

        TextView userStatusView = mview.findViewById(R.id.phone);
        userStatusView.setText(status);

    }


}

0 个答案:

没有答案