我有这个POJO:
public class Person{
private String id;
private List phoneNumbers=new ArrayList<>();
private Map<String,String> fcmTokens;
//getters + Setters + default constructor are present
}
在活动中: 我尝试使用以下代码将此对象保存到Firebase Realtime DB,但仅保存了String id值!我在这里想念什么?
Person p= new Person();
p.setUid("123");
List<String> list=new ArrayList<>();
list.add("123);
list.add("23");
p.setPhoneNumbers(list);
Map<String,String> map=new HashMap<>();
map.put("asdf","asdfas");
map.put("23","@3r");
p.setFcmTokens(map);
mPersonNodeReference.setValue(p);
答案 0 :(得分:0)
检查是否没有添加调用DataSnapshot.getValue(User.class)所需的Default构造函数
public class Person{
public Person() {
}
}
备注:变量命名不一致。
答案 1 :(得分:0)
要在Firebase中添加此类嵌套字段,应远离数组,并根据官方文档使用Map
。因此,对于您的情况,应该是
Person p = new Person();
p.setUid("123");
//List<String> list = new ArrayList<>(); //this will not work unless you push a new key for every value added to the list
//list.add("123");
//list.add("23");
Map<String,String> mapPhoneNumbers = new HashMap<>();
mapPhoneNumbers.put("1","123");
mapPhoneNumbers.put("2","23");
p.setPhoneNumbers(mapPhoneNumbers);
Map<String,String> map = new HashMap<>();
p.setFcmTokens(map) //not sure what barber is?
mPersonNodeReference.setValue(p); //did you mean p?
答案 2 :(得分:0)
A。活动中:
@Override
public void onCreate() {
super.onCreate();
FirebaseApp.initializeApp(this);
//your code.
}
B。应用程序级build.gradle:
apply plugin: 'com.android.application'
enter code here
android {
// ...
}
dependencies {
// ...
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
}
// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
C。项目级build.gradle:
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:4.0.0'
/*classpath 'com.google.gms:google-services:4.1.0' <-- do not use this version*/
}