.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) {
}
});
Read the Firebase docs for more information.