Android 如何在firebase Live数据库中保存模型?
我无法在数据库中保存数据, 我在网站上按照他们的指南做每件事,而网站上的数据库没有更新,我做错了什么 以下是我的功能:Android 如何在firebase Live数据库中保存模型?,android,Android,我无法在数据库中保存数据, 我在网站上按照他们的指南做每件事,而网站上的数据库没有更新,我做错了什么 以下是我的功能: public void updateUser() { // Write a message to the database FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference myRef = database.getRefere
public void updateUser() {
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("users");
myRef.setValue(appUser);
}
这是我的用户类:
公共类用户{
public User () {
}
public User(String username, String password, String deviceId, String email, String gender, String lookingFor, String shortDescription, String longDescription, String city, String country, String phone, ArrayList<Integer> profilePictures, int defaultPicture, int birthYear, int birthMonth, int birthDay) {
this.username = username;
this.password = password;
this.deviceId = deviceId;
this.email = email;
this.gender = gender;
this.lookingFor = lookingFor;
this.shortDescription = shortDescription;
this.longDescription = longDescription;
this.city = city;
Country = country;
this.phone = phone;
this.profilePictures = profilePictures;
this.defaultPicture = defaultPicture;
this.birthYear = birthYear;
this.birthMonth = birthMonth;
this.birthDay = birthDay;
}
public String username;
public String password;
public String deviceId;
public String email;
public String gender;
public String lookingFor;
public String shortDescription;
public String longDescription;
public String city;
public String Country;
public String phone;
public ArrayList<Integer> profilePictures;
public int defaultPicture;
public int birthYear;
public int birthMonth;
public int birthDay;
}
公共用户(){
}
公共用户(字符串用户名、字符串密码、字符串设备ID、字符串电子邮件、字符串性别、字符串查找、字符串短描述、字符串长描述、字符串城市、字符串国家、字符串电话、ArrayList profilePictures、int defaultPicture、int生日年、int生日月、int生日){
this.username=用户名;
this.password=密码;
this.deviceId=deviceId;
this.email=电子邮件;
这个。性别=性别;
this.lookingFor=寻找;
this.shortDescription=shortDescription;
this.longDescription=longDescription;
this.city=城市;
国家=国家;
this.phone=电话;
this.profilePictures=profilePictures;
this.defaultPicture=defaultPicture;
this.birthYear=生日;
this.birthMonth=birthMonth;
这个生日=生日;
}
公共字符串用户名;
公共字符串密码;
公共字符串设备ID;
公共字符串电子邮件;
社会性别;
公共字符串查找;
公共字符串短描述;
公共字符串描述;
公共字符串城市;
公共字符串国家;
公用串电话;
公共ArrayList简介图片;
公共图片;
公众生日;
公众生日月;
公众生日;
}
并且数据库表仍然没有更新:
我做错了什么
谢谢将updateUser()更改为以下内容:
public void updateUser() {
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
//Create a child reference
DatabaseReference userRef = myRef.child("users");
//pass the model value to the child reference
userRef.setValue(appUser);
}
将updateUser()更改为以下内容:
public void updateUser() {
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
//Create a child reference
DatabaseReference userRef = myRef.child("users");
//pass the model value to the child reference
userRef.setValue(appUser);
}
第一次尝试插入数据时,似乎出现了一些问题。用鼠标悬停在DB名称上时得到的十字按钮删除以前的数据 然后使用
child()
插入如下数据
public void updateUser() {
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
//getReference().child() is important
DatabaseReference myRef = database.getReference().child("users");
//This will generate a new key. Sort of like the key of a list item
String key = myRef.push().getKey();
myRef.child(key).setValue(appUser);
//Add a ValueEventListener to check whether the data you inserted worked or not
myRef.child(key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Data inserted
}
@Override
public void onCancelled(DatabaseError databaseError) {
//error
}
});
}
注意-还要检查您是否尝试插入appUser的空值。这似乎是您第一次尝试插入数据时出现了一些问题。用鼠标悬停在DB名称上时得到的十字按钮删除以前的数据 然后使用
child()
插入如下数据
public void updateUser() {
// Write a message to the database
FirebaseDatabase database = FirebaseDatabase.getInstance();
//getReference().child() is important
DatabaseReference myRef = database.getReference().child("users");
//This will generate a new key. Sort of like the key of a list item
String key = myRef.push().getKey();
myRef.child(key).setValue(appUser);
//Add a ValueEventListener to check whether the data you inserted worked or not
myRef.child(key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Data inserted
}
@Override
public void onCancelled(DatabaseError databaseError) {
//error
}
});
}
注意-还要检查您是否试图插入appUser的空值。您可以在侦听器所在的位置共享代码吗?例如:addValueEventListener或AddOnCompleteListener我可以分享一切,只是我不明白你的意思,什么的倾听者?是的,请分享。侦听器可用于更新firebase实时数据库。这就是为什么我引用了我前面提到的例子。或者你也可以在有监听器的地方分享代码?例如:addValueEventListener或AddOnCompleteListener我可以分享一切,只是我不明白你的意思,什么的倾听者?是的,请分享。侦听器可用于更新firebase实时数据库。这就是为什么我引用了我前面提到的例子。或者只是以防万一,在设置值之前您是否尝试过按<代码>myRef.push().setValue(appUser)将在数据库中创建一个新实例,并为该对象实例返回一个唯一的键id。以防万一,在设置该值之前您是否尝试推送<代码>myRef.push().setValue(appUser)将在数据库中创建一个新实例,并返回该对象实例的唯一键id。