【问题标题】:How to get all strings from firebase database如何从firebase数据库中获取所有字符串
【发布时间】:2018-06-02 14:38:53
【问题描述】:

我正在尝试将我的 firebase 数据库的所有子项存储到一个数组列表中。 这是我的数据库结构:

我目前正在尝试遍历子项以获得我需要的值,如下所示:

private void initializeData(int Destination) {
    listItems = new ArrayList<>();

    DatabaseReference MyRef = FirebaseDatabase.getInstance().getReference("rideShare");

    switch (Destination) {
        case 0:
            Toast.makeText(getActivity(), "LA BUNDLE", Toast.LENGTH_SHORT).show();
            MyRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    // This method is called once with the initial value and again
                    // whenever data at this location is updated.
                    for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        String NameValue = snapshot.child("Name").getValue(String.class);
                        String Price = snapshot.child("Price").getValue(String.class);
                        String Type = snapshot.child("Trip Type").getValue(String.class);
                        String Date = snapshot.child("Date").getValue(String.class);
                        String Destination = "LA";
                        String Phone = snapshot.child("Phone Number").getValue(String.class);
                        listingItem newItem = new listingItem(NameValue, Type, Price, Date, Destination, Phone);
                        listItems.add(newItem);

                    }
                }

                @Override
                public void onCancelled(DatabaseError error) {
                    // Failed to read value
                    //Log.w(TAG , "Failed to read value.",error.toException());
                }
            });
            break;
    }
}

【问题讨论】:

  • 任何帮助将不胜感激!谢谢!
  • 获取值有问题?
  • 目前你遇到了什么错误??

标签: android firebase firebase-realtime-database


【解决方案1】:

将所有字符串变量添加到 POJO 类中,并将变量命名为与子节点键相同。直接使用snapshot.get(listingItem.class)。 参考这个https://stackoverflow.com/a/39861649/3860386

【讨论】:

    【解决方案2】:

    要获取这些值,请使用以下代码:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference rideShareRef = rootRef.child("rideShare");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            List<ListingItem> listItems = new ArrayList<>();
    
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String nameValue = ds.child("Name").getValue(String.class);
                String type = ds.child("Trip Type").getValue(String.class);
                String price = ds.child("Price").getValue(String.class);
                String date = ds.child("Date").getValue(String.class);
                String destination = "LA";
                String phone = ds.child("Phone Number").getValue(String.class);
                ListingItem newItem = new ListingItem(nameValue, type, price, date, destination, phone);
                listItems.add(newItem);
            }
            Log.d("TAG", listItems);
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    rideShareRef.addListenerForSingleValueEvent(eventListener);
    

    在编写 Java 代码时,最好使用Java Naming conventions。我相应地添加了此代码。

    正如您可能看到的,我在onDataChange() 方法中添加了listItems 的声明,否则由于此方法的异步行为,它总是null

    【讨论】:

      猜你喜欢
      • 2020-09-13
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      相关资源
      最近更新 更多