Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Firebase android中编写数组_Java_Android_Firebase - Fatal编程技术网

Java 在Firebase android中编写数组

Java 在Firebase android中编写数组,java,android,firebase,Java,Android,Firebase,我正试着把这封信写给我的Firebase 母公司 --0:John --1:蒂姆 --2:山姆 --3:本 我正在这样做 String[] names = {"John","Tim","Sam","Ben"}; myFirebaseRef.setValue(names); 它没有写任何东西 它使用Firebase的Chrome控制台工作 这不是您向firebase写入数组的方式吗 谢谢这个.setValue()方法需要一个列表,而不是数组 对应于的值的 JSON类型:布尔、长、双精度、映射、字

我正试着把这封信写给我的Firebase

母公司

--0:John

--1:蒂姆

--2:山姆

--3:本

我正在这样做

String[] names = {"John","Tim","Sam","Ben"};
myFirebaseRef.setValue(names);
它没有写任何东西

它使用Firebase的Chrome控制台工作

这不是您向firebase写入数组的方式吗

谢谢

这个
.setValue()
方法需要一个
列表
,而不是
数组

对应于的值的 JSON类型:布尔、长、双精度、映射、字符串、对象、列表、对象

现在如果你想检索数据,你只需要知道名字

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) {

    }
});
Firebase ref=newfirebase(“/names”):
Firebase johnRef=参考儿童(“约翰”);
johnRef.addValueEventListener(新的ValueEventListener(){
@凌驾
公共无效onDataChange(数据快照快照){
System.out.println(snapshot.value);//字符串“John”
}
@凌驾
取消后的公共无效(FirebaseError FirebaseError){
}
});

我对Java不太清楚,但对于web应用程序,您可以使用以下内容:

我想创建一个可重用的函数来添加/保存根节点下的任何对象(即使在对象中有任何级别的数据数组)。所以我想到了这个。(我不确定是否符合最佳实践,但它运行得相当顺利)

因此,要在root
users
下添加新用户,您可以调用:

SaveWholeData("users", oUserInfo, true);
要更新现有用户,请执行以下操作:

SaveWholeData("users"+"/"+ oUserInfo.genKey, oUserInfo, true);
List sendingList=newarraylist(Arrays.asList(SendingArray));

如果出现任何错误,请按Alt+Enter。它将根据您的android studio版本进行更正

谢谢您的回答,这会将其保存为0:John,1:Tim。。。或者约翰:约翰,提姆:提姆…?谢谢。这个答案包含了我一直在寻找的大部分信息。谢谢你,大卫!您好@David East谢谢您的回答,我需要有关firebase实时数据库中阵列的更多信息,我想执行删除字符串并将新字符串添加到现有阵列此回答刚刚保存了我的工作!!非常感谢!
    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;
        }
    }
SaveWholeData("users", oUserInfo, true);
SaveWholeData("users"+"/"+ oUserInfo.genKey, oUserInfo, true);
List sendingList = new ArrayList<>(Arrays.asList(SendingArray));