Writing array in Firebase android

前端 未结 3 2114
时光说笑
时光说笑 2021-02-07 14:45

I am trying to write this to my Firebase

Parent

--0 : John

--1 : Tim

--2 : Sam

--3 : Ben

I am doing this

String[         


        
相关标签:
3条回答
  • 2021-02-07 14:59
    List sendingList = new ArrayList<>(Arrays.asList(SendingArray));
    

    In case of any error, press Alt+Enter. It will correct it depending on you version of android studio

    0 讨论(0)
  • 2021-02-07 15:06

    The .setValue() method needs a List rather than an Array.

    The native types accepted by this method for the value correspond to the JSON types: Boolean, Long, Double, Map, String, Object, List, Object...

    Firebase ref = new Firebase("<my-firebase-app>/names"):
    String[] names = {"John","Tim","Sam","Ben"};
    List nameList = new ArrayList<String>(Arrays.asList(names));
    // Now set value with new nameList
    ref.setValue(nameList);  
    

    But, I recommend using a Map instead of a List. Rather than having an index based key (1,2,3...), you could use the name as the key so it's easier to retrieve.

    Firebase ref = new Firebase("<my-firebase-app>/names"):
    HashMap<String, String> names = new HashMap()<String, String>;
    names.put("John", "John");
    names.put("Tim", "Tim");
    names.put("Sam", "Sam");
    names.put("Ben", "Ben");
    ref.setValue(names);
    

    And now if you want to retrieve the data, you just need to know the name.

    Firebase ref = new Firebase("<my-firebase-app>/names"):
    Firebase johnRef = ref.child("john");
    johnRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
           System.out.println(snapshot.value); // the String "John"
        }
        @Override
        public void onCancelled(FirebaseError firebaseError) {
    
        }
    });
    

    Read the Firebase docs for more information.

    0 讨论(0)
  • 2021-02-07 15:24

    I am not sure about Java, but for web application, you can use something as follows:

    I wanted to create a reusable function to add/save any object under a root node (even if that has an array of data at any level within the object). So I came up with this. (I am not sure if conforms to the best practices, but it worked pretty smooth)

        SaveWholeData: function(sKey, oVal, bGenKey) {
            bGenKey = bGenKey === undefined ? true: bGenKey;
            var oOriginalProperty = angular.copy(oVal);
            for (var property in oVal) {
                if (oVal.hasOwnProperty(property) && oVal[property] instanceof Array) {
                    console.log(property);
                    oVal[property] = "$|$";
                }
            }
            var sOwnRef = SaveDataByKey(sKey, oVal, bGenKey);
            for (var property in oVal) {
                if (oVal.hasOwnProperty(property) && oVal[property] === "$|$") {
                    oVal[property] = oOriginalProperty[property];
                    var updatedReference = sOwnRef + "/" + property;
                   SaveWholeData(updatedReference, oVal[property], false);
                }
            }
            return true;
        },
        SaveDataByKey: function(sKey, oVal, bGenKey) {
            if (bGenKey) {
                var newPostKey = firebase.database().ref(sKey).push().key;
                oVal.genKey = newPostKey;
                firebase.database().ref(sKey).child(newPostKey).set(oVal);
                return sKey + "/" + newPostKey;
            }else{
                firebase.database().ref(sKey).set(oVal);
                return sKey;
            }
        }
    

    So to add new user under root users, you call:

    SaveWholeData("users", oUserInfo, true);
    

    and to update existing user:

    SaveWholeData("users"+"/"+ oUserInfo.genKey, oUserInfo, true);
    
    0 讨论(0)
提交回复
热议问题