【问题标题】:Firebase object to Java classFirebase 对象到 Java 类
【发布时间】:2021-05-24 20:28:20
【问题描述】:

我正在尝试从 relatime 数据库中读取以下对象:

.. 到这个 Java Category.class:

public class Category {
    private String nombre;
    private String descripcion;
    private String estado;
    private List<String> productos;
    private List<Page> paginas;

    public Category(){}

    public Category(String name) {
        this.nombre = "";
        this.descripcion = "";
        this.estado = "";
        productos = new ArrayList<>();
        paginas = new ArrayList<>();
    }

    public String getCategoryName() {
        return nombre;
    }

    public void setCategoryName(String nombre) {
        this.nombre = nombre;
    }

    public String getCategoryDescription() {
        return descripcion;
    }

    public void setCategoryDescription(String descripcion) {
        this.descripcion = descripcion;
    }

    public String getCategoryState() {
        return estado;
    }

    public void setCategoryState(String estado) {
        this.estado = estado;
    }

    public List<String> getProducts() {
        return this.productos;
    }

    public void setProducts(List<String> productos) {
        this.productos = productos;
    }

    public List<Page> getPages() {
        return this.paginas;
    }

    public void setPages(List<Page> pages) {
        this.paginas = paginas;
    }

    @Override
    public String toString() {
        return nombre;
    }
}

我已经从不同的来源搜索和阅读,根据我的理解,我应该使用地图,但事实是,我不知道该怎么做......

这是读取的代码,如果我调试一下,我可以看到dataSnaphot有我需要的一切,但是C类为null:

FirebaseDatabase.getInstance().getReference().child("categorias").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
            Category c = snapshot.getValue(Category.class);
            list.add(c);
        }
        AdapterCategories adapter = new AdapterCategories(list);
        recycleView.setAdapter(adapter);
    }

    @Override
    public void onCancelled(DatabaseError error) {
        // Failed to read value
        Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_LONG).show();
    }
});

这是从 FIREBASE 导出的 JSON:

"categorias" : {
    "Telas de Hogar" : {
      "descripcion" : "",
      "estado" : "invisible",
      "nombre" : "Telas de Hogar",
      "paginas" : {
        "Loneta" : {
          "descripcion" : "",
          "estado" : "invisible",
          "nombre" : "Loneta"
        }
      }
    }
  }

请谁能帮我解决这个问题。

【问题讨论】:

  • 您可以在数据库中同时存储 ListMap,但它们会产生不同的 JSON 结构。您可以编辑您的问题以在categorias 包含JSON(作为文本,请不要截图)?您可以通过单击Firebase Database console 上溢出菜单 (⠇) 中的“导出 JSON”链接来获取此信息。
  • 乍一看像descripcion 这样的属性似乎映射得很好。你能Log.d("DB", snapshot.getValue())吗?并在问题中包含更新的代码及其输出?

标签: java android firebase class firebase-realtime-database


【解决方案1】:

您可以在数据库中同时存储 List 和 Map,但它们会产生不同的 JSON 结构。


如果你在其中存储一个带有List&lt;String&gt; products 的对象,那么它在数据库中的结果会是这样的:

"products": {
  "0": "Product one",
  "1": "Product two",
  "2": "Product three"
}

您会注意到 Firebase 生成的键是序列号,存储为字符串值(因为所有键都存储为字符串)。这就是 Firebase 在数据库中表示 List 或数组的方式。

因此,如果您从数据库中读取这样的 JSON,它会映射到 List&lt;String&gt;


如果您的结构中的键不是连续数字,例如通过调用 push() 生成的键,它可能如下所示:

"paragraphs": {
  "-M...1": "He was an old man who fished alone in a skiff in the Gulf Stream...",
  "-M...2": "The old man was thin and gaunt with deep wrinkles in...",
  "-M...3": "Everything about him was old except his..."
}

此 JSON 结构映射到 Java 中的 Map&lt;String, String&gt;,因为无法将键转换为序号。

【讨论】:

  • 吸气剂也有问题。 nombre 属性的 getter 应该是 getNombre() 而不是 getCategoryName()
  • 很好,亚历克斯。我没有检查category,因为无论如何在Telas de Hogar 下的JSON 中没有这样的属性。
  • 所以我必须将 get/set + 属性名称准确地放在 Firebase 中?
  • 默认情况下,Firebase 就是这样知道将哪些 JSON 属性映射到 Java 对象中的哪些字段/getter/setter。如果您想控制映射,可以添加@PropertyName annotation。请参阅stackoverflow.com/q/38681260stackoverflow.com/a/45330351stackoverflow.com/a/30943084
  • 请不要以隐藏先前问题的方式更改您的问题,因为它已经过时了答案。我们正在尝试在这里建立一个知识库,所以我的回答仍然与您最初的问题相匹配很重要。
猜你喜欢
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 2011-02-08
  • 2019-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多