【问题标题】:How to show datas on a ListView when it has multiple child in Firebase database as shownin picture如图所示,当 Firebase 数据库中有多个子项时如何在 ListView 上显示数据
【发布时间】:2018-07-27 08:19:27
【问题描述】:

所以请看看我面临的问题。

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 如果它在键下有值,那么我可以做到,但是在键下,它有 0 然后值,我没有得到它

标签: java android firebase listview firebase-realtime-database


【解决方案1】:

假设 SuccessfulOrders 节点是 Firebase 数据库根的直接子节点,要获取该数据并将其添加到列表中,请使用以下代码:

String phoneNo = FirebaseAuth.getInstance().getCurrentUser().getPhoneNumber();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference phoneNoRef = rootRef.child("SuccessfulOrders").child(phoneNo);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        ListView<String> list = new ArrayList<>();
        for(DataSnapshot dSnapshot : dataSnapshot.getChildren()) {
            for(DataSnapshot ds : dSnapshot.getChildren()) {
                String productName = ds.child("productName").getValue(String.class);
                list.add(productName);
                Log.d("TAG", productName);
            }

            //Do what you need to do with your list.
            Log.d("TAG", list.toString());
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
phoneNoRef.addListenerForSingleValueEvent(valueEventListener);

logat 中的输出将是:

1000 Website Visit
SOCIAL MEDIA PACK
//and so on

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-10
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2021-12-23
    相关资源
    最近更新 更多