【发布时间】:2020-12-22 23:23:13
【问题描述】:
我在尝试更新到 Firebase 实时数据库时遇到问题。
我收到错误“com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead
我认为这可能意味着我需要使用另一种方式来更新数据,因为我使用的是 recyclerView 而不是 listView,但现在我有了:
public void update_button(){
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
snapshot.getRef().child("title").setValue(postTitleEdit.getText());
snapshot.getRef().child("desc").setValue(postTextEdit.getText());
Toast.makeText(CowDet.this, "updated", Toast.LENGTH_LONG).show();
CowDet.this.finish();
}
我用来向 firebase 添加数据的方式是:
mDatabaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
Blog blog = snapshot.getValue(Blog.class);
blogList.add(blog);
blogRecyclerAdapter = new BlogRecyclerAdapter(PostListActivity.this, blogList);
recyclerView.setAdapter(blogRecyclerAdapter);
blogRecyclerAdapter.notifyDataSetChanged();
}
有人可以帮我更新数据吗?我有点卡在这里。
这是我的博客类:
public class Blog {
public String title;
public String desc;
public String image;
public String timestamp;
public String userid;
public Blog() {
}
public Blog(String title, String desc, String image, String timestamp, String userid, String milkQty, String note) {
this.title = title;
this.desc = desc;
this.image = image;
this.timestamp = timestamp;
this.userid = userid;
}
public Blog(String title, String desc) {
this.title = title;
this.desc = desc;
this.image = image;
this.timestamp = timestamp;
this.userid = userid;
}
public Blog(String toString) {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
}
新的更新按钮
public void update_button(){
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
databaseReference.child("Mblog").child("title").setValue(postTitleEdit);
Toast.makeText(CowDet.this, "updated", Toast.LENGTH_LONG).show();
CowDet.this.finish();
}
新错误:com.google.firebase.database.DatabaseException:发现名称冲突的 getter:getText
我在databaseReference.child("Mblog").child("title").setValue(postTitleEdit);
【问题讨论】:
-
我试图在下面回答,但如果您确保您的问题包含所谓的minimal, complete/standalone example,我们中的任何人都可以使用它来重现问题,那么它会更容易提供帮助。这将包括所有必要的代码、数据和你得到的错误的完整堆栈跟踪。
-
嗨@FrankvanPuffelen 我已经添加了我的博客类,但我没有任何 [] 数组
-
那么错误可能来自其他地方。您可以编辑问题以显示整个堆栈跟踪吗?
-
@FrankvanPuffelen 我对更新的方式进行了一些更改,现在我收到了错误 com.google.firebase.database.DatabaseException:发现名称冲突的 getter:getText,我已经更新了问题,最后是新的错误
-
这是一个不同的错误,所以是一个不同的问题。但是请先搜索错误信息,因为它已经被多次覆盖了。
标签: java android firebase firebase-realtime-database