首页 > 解决方案 > 更新到 Firebase 数据库时出错

问题描述

我在尝试更新到 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);

标签: javaandroidfirebasefirebase-realtime-database

解决方案


错误消息非常明确。您的Blog类似乎包含一个数组(带有[]),Firebase 的序列化程序不支持该数组。你应该使用一个List类来达到同样的目的。

如果您很难完成这项工作,请编辑您的问题以包含Blog课程。


推荐阅读