Android-如何从多个节点检索数据并将其显示在单个列表中?

时间:2020-09-23 13:29:24

标签: android firebase-realtime-database

我正在尝试从多个节点检索数据并将它们存储在单个列表中。我可以这样做,但是问题是如果删除函数list.clear();中的getTeam2(),数据就会重复。

这是数据库的屏幕截图。

enter image description here

以下是代码

 databaseReference = FirebaseDatabase.getInstance().getReference("Teams")
            .child(team1).child("Players");

    databaseReference.orderByChild("playertype").equalTo("Wk")
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    mList.clear();
                    for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                        Player player = dataSnapshot.getValue(Player.class);
                        mList.add(player);
                        getTeam2(mList);
                    }
                }

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

                }
            });
    return view;
}

private void getTeam2(final List<Player> mList) {
    DatabaseReference team = FirebaseDatabase.getInstance().getReference("Teams")
            .child(team2).child("Players");
    team.orderByChild("playertype").equalTo("Wk")
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                        Player player = dataSnapshot.getValue(Player.class);
                        mList.add(player);
                        bar.setVisibility(View.INVISIBLE);
                    }
                    adapter = new PlayerAdapter(getContext(), WKFragement.this.mList);
                    recyclerView.setAdapter(adapter);
                }

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

                }
            });
}

如果我在list.clear();函数中使用team2,那么它将仅显示team2的成员。

我应该进行哪些更改?

0 个答案:

没有答案