gpt4 book ai didi

android - 将 FireStore 集合映射到 Java POJO

转载 作者:搜寻专家 更新时间:2023-11-01 08:24:21 25 4
gpt4 key购买 nike

我正在使用 FireStore 数据库,我想知道如何将这些集合从“Hospital/DcbtizNr0ADNNnd0evlN”转换为 Java 对象?我的意思是,我如何搜索(使用 1 个查询)特定医院并将其所有文档和集合映射到 Java 对象?

This is my database structure

Pacientes/Fichas/Profissionais 遵循几乎相同的结构:

enter image description here

最佳答案

假设您的应用包含与数据匹配的 Hospital POJO 类,您可以轻松地将 QuerySnapshot 映射到对象列表:

db.collection("Hospital")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
// Convert the whole snapshot to a POJO list
List<Hospital> objects = task.getResult().toObjects(Hospital.class);
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});

医院类可能如下所示:

public class Hospital {

// These can either be public properties, or private properties
// with standard-named getters and setters like getAvenida() and
// setAvenida()
public String avenida;
public String bairro;
public String cep;
public String cidade;
public String estado;
public String name;
public String numero;

// Empty constructor required
Hospital() {}

}

引用: https://firebase.google.com/docs/firestore/reference/android/QuerySnapshot.html#toObjects(java.lang.Class )

关于android - 将 FireStore 集合映射到 Java POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46779490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com