来自不同大小和数据类型Firebase的多个节点的流程数据

时间:2016-07-25 05:24:48

标签: android json firebase firebase-realtime-database

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

piggyWord

到目前为止,我已尝试过

private static String piggyWord(String word) {
        int split = firstVowel(word);
        if(beginsWithVowel(word)) {
            return word.substring(split + 1) + word.substring(0, split + 1); //Since vowel is in 1st place, substring(0,0) returns empty string.
        } else {
        return word.substring(split) + word.substring(0, split)+"ae";
        }
}   

打印:

 -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

我如何将这些数据解析为不同的类型,而没有明确说明我要从每个节点查找哪种数据类型?这是可能的还是我应该做一个大的switch语句并用它的键来处理每个项目?

1 个答案:

答案 0 :(得分:0)

我找到的解决方案是制作一个像这样的大型开关语句:

    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++;

        }
    }