从Firebase检索一些数据并将其放在RecyclerView上

时间:2019-03-04 08:31:45

标签: java android firebase android-recyclerview

如何从RecyclerView中的Firebase中检索 Rute 数据,其中 Sofer 用户显示名称是否相等?

这是我的Firebase的一部分。我尝试这样做,但是出现了所有数据。

public void onStart() {
    super.onStart();

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference ruteRef = rootRef.child("Rute");
    assert user != null;
    Query query = ruteRef.orderByChild("Sofer").equalTo(user.getDisplayName());
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(Model.class, R.layout.card_layout, ViewHolder.class, mRef) {
                @Override
                protected void populateViewHolder(ViewHolder viewHolder, Model model, final int position) {

                    viewHolder.setDetails(getContext(), model.getTaraPlecare(), model.getTaraDestinatie(), model.getSofer(),
                            model.getCapTractor(), model.getRemorca(), model.getNumeleIncarcaturii(), model.getDataPlecare(),
                            model.getOrasPlecare(), model.getOrasDestinatie());
                    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // Intent intent = new Intent(Fridge.this, Show.class);
                            //  startActivity(intent);
                        }
                    });
                }
            };

            RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 1);
            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.setAdapter(firebaseRecyclerAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Toast.makeText(getContext(), "Eroare", Toast.LENGTH_LONG).show();
        }
    });

}

2 个答案:

答案 0 :(得分:1)

  

在Sofer等于用户显示名称的recyclerview中,如何从Firebase中检索Rute数据?

只需通过执行以下查询即可:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ruteRef = rootRef.child("Rute");
Query query = ruteRef.orderByChild("Sofer").equalTo("userDisplayName");
query.addListenerForSingleValueEvent(/* ... */);

this 是一种推荐的方式,您可以从Firebase Realtime数据库中检索数据并使用RecyclerViewFirebaseRecyclerAdapter中显示它们。

答案 1 :(得分:0)

我建议您将现有结构替换为:

Rute:
    [
        { "Username1": 
           //All your data related to Username
        },
        { "Username2": 
           //All your data related to Username
        },
        { "Username3": 
           //All your data related to Username
        }
    ]

以上结构将帮助您实现所需的目标。