【问题标题】:Process Data From Multiple Nodes of Varying Size and Data Type Firebase处理来自不同大小和数据类型 Firebase 的多个节点的数据
【发布时间】:2025-11-22 08:45:01
【问题描述】:

这是我的 Firebase 数据库的“moods”节点在 Firebase 控制台上的样子,其中第一个节点完全展开。我想通过数据将前三个作为字符串,将“位置”键作为 int,将“场所”作为数组或 ArrayList,理想情况下不通过其键引用每个单独的元素(例如“KIsbAUkPflh5JH_ig24” )。我想对每个元素都这样做。

 -moods
 -KIsbAUkPflh5JH_ig24
      icon: "http://www.imagesource.com/0"
      image: "http://www.imagesource.com/1"
      name:  "Happy"
      position:  1
 -venues 
      100: true
      262: true
      45: false
      68: false
      82: true
 +KIxKyLALCKFV67azkK7
 +KIxL-z474lwJ2aR86NU
 +KIxQQOow2pVeMeqVpL1
 +KIxU-Umxac4aG_swuNt

到目前为止我已经尝试过

public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.

  Log.d(TAG, dataSnapshot.toString());
  Log.d(TAG, "ChildrenCount == " + dataSnapshot.getChildrenCount());
  for(DataSnapshot d : dataSnapshot.getChildren())
   {
        Log.d(TAG, d.getKey() + " : " + d.getValue(String.class));
   }
   dataSnapshot.getValue(ArrayList.class);
 }

哪些打印:

(FULL JSON DOCUMENT ON THIS LINE)

ChildrenCount == 5

icon : http://www.imagesource.com/0

image : http://www.imagesource.com/1

name : Happy

D/AndroidRuntime: Shutting down VM

E/UncaughtException: com.google.firebase.database.DatabaseException: Failed to >convert value of type java.lang.Long to String

在不明确说明我要从每个节点查找的数据类型的情况下,我将如何将这些数据解析为不同的类型?这是可能的还是我应该只做一个大的 switch 语句并按其键处理每个项目?

【问题讨论】:

  • 我觉得应该是 Log.d(TAG, d.getKey() + " : " + d.getValue().toString());因为您的行正在将 long 转换为字符串,这会给您带来错误。
  • 你的数据结构不清楚。请包含实际的 JSON 作为文本,您可以通过单击 Firebase 控制台中的导出按钮来获取。

标签: android json firebase firebase-realtime-database


【解决方案1】:

我找到的解决方案是像这样写一个大的 switch 语句:

    public void onDataChange(DataSnapshot dataSnapshot) {
        // This method is called once with the initial value and again
        // whenever data at this locations is updated.

        int childCount = (int)dataSnapshot.getChildrenCount();

        final String [] icons = new String[childCount];
        final String [] images = new String[childCount];
        /*final String []*/ moodNames = new String[childCount];
        final Bitmap [] backgroundBitmaps = new Bitmap[childCount];
        venueKeysInEachButton = new String[childCount][];
        int moodCounter = 0;

        //For each mood in moods
        for(DataSnapshot mood :  dataSnapshot.getChildren()) {

            //Get all of the information about the mood
            String icon = "";
            String image = "";
            String moodName = "";
            int position = 0;

            for (DataSnapshot moodChild : mood.getChildren()) {

                String key = moodChild.getKey();
                switch (key) {
                    case "icon":
                        icon = moodChild.getValue(String.class);
                        Log.i(TAG, key + " : " + icon);
                        break;
                    case "image":
                        image = moodChild.getValue(String.class);
                        Log.i(TAG, key + " : " + image);
                        break;
                    case "name":
                        moodName = moodChild.getValue(String.class);
                        Log.i(TAG, key + " : " + moodName);
                        break;
                    case "position":
                        position = moodChild.getValue(Long.class).intValue() - 1;
                        Log.i(TAG, key + " : " + position);
                        break;
                    case "venues":

                        Log.i(TAG, key + " : " + moodChild.getValue().toString());

                        ArrayList<String> venueKeys = new ArrayList<String>();
                        Iterable<DataSnapshot> venueSnapshot = moodChild.getChildren();
                        for (DataSnapshot venue : venueSnapshot){
                            Log.d(TAG, venue.toString());
                            if(venue.getValue(Boolean.class)){
                                venueKeys.add(venue.getKey());
                            }
                        }

                        venueKeysInEachButton[moodCounter] = new String [venueKeys.size()];
                        venueKeys.toArray(venueKeysInEachButton[moodCounter]);
                        break;
                    default:
                        Log.e(TAG, "Error when parsing DataSnapshot, key is : " + key);
                        break;
                }
            }

            icons[position] = icon;
            images[position] = image;
            moodNames[position] = moodName;
            moodCounter++;

        }
    }

【讨论】: