Android - Firebase Ui从其他表中获取数据?

时间:2017-10-25 04:25:34

标签: android firebase firebase-realtime-database firebaseui

大家好我需要从uid

的其他表引用中获取user_fname

enter image description here

enter image description here

在我的代码下面 我使用的是IndexedQuery,而不是从教程和示例代码Firebase Ui

中清除
mRef = FirebaseDatabase.getInstance().getReference().child("mainboard");
mReference = FirebaseDatabase.getInstance().getReference().child("user");
mRecyclerView = view.findViewById(R.id.mainboard_alllist);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mLayoutManager = new LinearLayoutManager(getContext());
mOption = new FirebaseRecyclerOptions.Builder<Mainboard>().
            setIndexedQuery(mRef , mReference  ,Mainboard.class)
            .setLifecycleOwner(this)
            .build();

mAdapter = new FirebaseRecyclerAdapter<Mainboard, MainboardViewHolder>(mOption) {
    @Override
    public MainboardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mainboard_list , parent , false);
        return new MainboardViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(MainboardViewHolder holder, int postion, Mainboard mainboard) {
        String mb_id = mainboard.mb_id;
        mRef.child( mb_id + "/mb_auther").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String mb_author = dataSnapshot.getValue().toString();
                mReference.child(mb_author + "/user_fname").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        String user_fname = dataSnapshot.getValue().toString();
                        Log.e("user_fname" , dataSnapshot.getValue().toString());
                        holder.setAuthor(user_fname);
                    }
                }
            }
        };
    }
}

1 个答案:

答案 0 :(得分:0)

直接方法

1.首先使用mb_auther侦听器获取addListenerForSingleValueEvent

2.然后使用mb_auther值获取user_fname

  mRef.child("id/mb_auther").addListenerForSingleValueEvent(new ValueEventListener() { //Your case id is -KrHOUwrsBbXHvUaOTIu
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String mb_auther = dataSnapshot.getValue().toString();
            mReference.child(mb_auther + "/user_fname").addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    String user_fname = dataSnapshot.getValue().toString()
                    Log.e("user_fname", user_fname);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });