我正在尝试将此内容写入我的Firebase
父
- 0:约翰
- 1:蒂姆
- 2:Sam
- 3:Ben
我这样做
Player(int number,
int goals,
std::string position,
std::string feet,
const char* name,
std::string country,
int height) : TournamentMember(name, country, height)
{
// constructor body
// any member variables set here can be set along with TournamentMember
// in the initializer list. Often this means the constructor's body is
// completely empty.
}
它没有写任何东西。
使用适用于Firebase的Chrome控制台。
这不是你把数组写入firebase的方式吗?
由于
答案 0 :(得分:25)
.setValue()
方法需要List
而不是Array
。
值的native types accepted by this method对应于 JSON类型: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);
但是,我建议使用Map
代替List
。您可以使用名称作为键,而不是使用基于索引的键(1,2,3 ...),因此更容易检索。
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);
现在,如果您想要检索数据,您只需要知道名称。
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) {
}
});
答案 1 :(得分:0)
我不确定Java,但对于Web应用程序,您可以使用以下内容:
我想创建一个可重用的函数来添加/保存根节点下的任何对象(即使它在对象内的任何级别都有一个数据数组)。所以我想出了这个。 (我不确定是否符合最佳实践,但它的工作非常顺利)
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;
}
}
所以要在根users
下添加新用户,请致电:
SaveWholeData("users", oUserInfo, true);
以及更新现有用户:
SaveWholeData("users"+"/"+ oUserInfo.genKey, oUserInfo, true);
答案 2 :(得分:0)
List sendingList = new ArrayList<>(Arrays.asList(SendingArray));
如果有任何错误,请按 Alt + Enter 。它将根据您的android studio版本进行纠正