将 DataSnapshot 中的数据编组到 Android studio 中的自定义 class
Marshall data from DataSnapshot into custom class in Android studio
我似乎无法弄清楚如何将 datasnapshot 中的数据读入我自己的 class。
这是我尝试这样做的函数:
private void initializeFirebase(){
my_db = FirebaseDatabase.getInstance();
DatabaseReference my_ref = my_db.getReference();
Map<String, ?> values = sharedPreferences.getAll();
if (!values.isEmpty()){
final String id = Profile.getCurrentProfile().getId();
my_ref = my_ref.child("users").child("userid").child(id);
if (my_ref != null) my_ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
value = dataSnapshot.getValue(UserData.class);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
mergeClassToSharedPreferences();
}
else {
for (Map.Entry<String, ?> entry : values.entrySet()) {
my_ref.child("userid").child(Profile.getCurrentProfile().
getId()).child(entry.getKey()).setValue(entry);
}
}
}
这是我写的class:
public class UserData {
public String age, customized_message, dreams_response, gender, intro_response,
rpp_phone_number, substances_response;
public boolean relapse_prevention_program, firstrun;
public int distance;
public UserData(){
}
public UserData(String age, String customized_message, String dreams_response, String gender,
String intro_response, String rpp_phone_number, String substances_response,
boolean relapse_prevention_program, boolean firstrun, int distance){
this.age = age;
this.customized_message = customized_message;
this.dreams_response = dreams_response;
this.gender = gender;
this.intro_response = intro_response;
this.rpp_phone_number = rpp_phone_number;
this.substances_response = substances_response;
this.relapse_prevention_program = relapse_prevention_program;
this.firstrun = firstrun;
this.distance = distance;
}
public String getAge(){
return age;
}
public String getCustomized_message(){
return customized_message;
}
public String getDreams_response(){
return dreams_response;
}
public String getGender(){
return gender;
}
public String getIntro_response(){
return intro_response;
}
public String getRpp_phone_number(){
return rpp_phone_number;
}
public String getSubstances_response(){
return substances_response;
}
public boolean getRelapse_prevention_program(){
return relapse_prevention_program;
}
public boolean getFirstrun(){
return firstrun;
}
public int getDistance(){
return distance;
}
public void setDreams_response(String dreams_response){
this.dreams_response = dreams_response;
}
public void setGender(String gender){
this.gender = gender;
}
public void setIntro_response(String intro_response){
this.intro_response = intro_response;
}
public void setRpp_phone_number(String rpp_phone_number){
this.rpp_phone_number = rpp_phone_number;
}
public void setAge(String age){
this.age = age;
}
public void setCustomized_message(String customized_message){
this.customized_message = customized_message;
}
public void setSubstances_response(String substances_response){
this.substances_response = substances_response;
}
public void setRelapse_prevention_program(boolean relapse_prevention_program){
this.relapse_prevention_program = relapse_prevention_program;
}
public void setFirstrun(boolean firstrun){
this.firstrun = firstrun;
}
public void setDistance(int distance){
this.distance = distance;
}
}
我相信我正在正确访问我的树,但每次我尝试 运行 这段代码时,它只是报告 "W/ClassMapper: No setter/field for key or value found on class"。
enter image description here
您在实时数据库中对数据建模的方式与 SDK 执行自动序列化的方式不兼容。它只接受 key/value 对,其中键映射到对象中的字段,值匹配每个字段可以包含的数据类型。您的数据结构化为每个字段的对象。如果您使用此结构,则需要编写自定义代码以将所有数据从快照中取出并放入您的 POJO 中。
我似乎无法弄清楚如何将 datasnapshot 中的数据读入我自己的 class。
这是我尝试这样做的函数:
private void initializeFirebase(){
my_db = FirebaseDatabase.getInstance();
DatabaseReference my_ref = my_db.getReference();
Map<String, ?> values = sharedPreferences.getAll();
if (!values.isEmpty()){
final String id = Profile.getCurrentProfile().getId();
my_ref = my_ref.child("users").child("userid").child(id);
if (my_ref != null) my_ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
value = dataSnapshot.getValue(UserData.class);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
mergeClassToSharedPreferences();
}
else {
for (Map.Entry<String, ?> entry : values.entrySet()) {
my_ref.child("userid").child(Profile.getCurrentProfile().
getId()).child(entry.getKey()).setValue(entry);
}
}
}
这是我写的class:
public class UserData {
public String age, customized_message, dreams_response, gender, intro_response,
rpp_phone_number, substances_response;
public boolean relapse_prevention_program, firstrun;
public int distance;
public UserData(){
}
public UserData(String age, String customized_message, String dreams_response, String gender,
String intro_response, String rpp_phone_number, String substances_response,
boolean relapse_prevention_program, boolean firstrun, int distance){
this.age = age;
this.customized_message = customized_message;
this.dreams_response = dreams_response;
this.gender = gender;
this.intro_response = intro_response;
this.rpp_phone_number = rpp_phone_number;
this.substances_response = substances_response;
this.relapse_prevention_program = relapse_prevention_program;
this.firstrun = firstrun;
this.distance = distance;
}
public String getAge(){
return age;
}
public String getCustomized_message(){
return customized_message;
}
public String getDreams_response(){
return dreams_response;
}
public String getGender(){
return gender;
}
public String getIntro_response(){
return intro_response;
}
public String getRpp_phone_number(){
return rpp_phone_number;
}
public String getSubstances_response(){
return substances_response;
}
public boolean getRelapse_prevention_program(){
return relapse_prevention_program;
}
public boolean getFirstrun(){
return firstrun;
}
public int getDistance(){
return distance;
}
public void setDreams_response(String dreams_response){
this.dreams_response = dreams_response;
}
public void setGender(String gender){
this.gender = gender;
}
public void setIntro_response(String intro_response){
this.intro_response = intro_response;
}
public void setRpp_phone_number(String rpp_phone_number){
this.rpp_phone_number = rpp_phone_number;
}
public void setAge(String age){
this.age = age;
}
public void setCustomized_message(String customized_message){
this.customized_message = customized_message;
}
public void setSubstances_response(String substances_response){
this.substances_response = substances_response;
}
public void setRelapse_prevention_program(boolean relapse_prevention_program){
this.relapse_prevention_program = relapse_prevention_program;
}
public void setFirstrun(boolean firstrun){
this.firstrun = firstrun;
}
public void setDistance(int distance){
this.distance = distance;
}
}
我相信我正在正确访问我的树,但每次我尝试 运行 这段代码时,它只是报告 "W/ClassMapper: No setter/field for key or value found on class"。 enter image description here
您在实时数据库中对数据建模的方式与 SDK 执行自动序列化的方式不兼容。它只接受 key/value 对,其中键映射到对象中的字段,值匹配每个字段可以包含的数据类型。您的数据结构化为每个字段的对象。如果您使用此结构,则需要编写自定义代码以将所有数据从快照中取出并放入您的 POJO 中。