我收到此错误消息



当我尝试将文件从Firestore读取到自定义对象DBStructure.class中时。

这是我用来检索数据的代码:


    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private DocumentReference DBStructureDocRef = db.document(DB_STRUCTURE_PATH);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRoom = new Room();
        mDBStructure = new DBStructure();

        readData();
        HandleSpinnerSelection("", -1);
    }

    private void readData() {
        DBStructureDocRef.get()
                .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                        if (documentSnapshot.exists()) {
                            **mDBStructure = documentSnapshot.toObject(DBStructure.class)**;
                        }
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(MainActivity.this, "Error!", Toast.LENGTH_SHORT).show();
                        Log.d(TAG, e.toString());
                    }
                });
    }

这是DBStructure.class。
    public class DBStructure {

    List<String> allTypes = new ArrayList<>();
    List<String> allNames = new ArrayList<>();
    List<String> allSurfaces = new ArrayList<>();
    Map<String, Map<String, Unit>> hospitalLayout = new HashMap<>();

    public DBStructure() {
    }

    public DBStructure(String allTypes, String allNames, List<String> allSurfaces, Map<String, Map<String, Unit>> hospitalLayout) {
        setAllTypes(allTypes);
        setAllNames(allNames);
        setAllSurfaces(allSurfaces);
        this.hospitalLayout = hospitalLayout;
    }

    public List<String> getAllTypes() {
        return allTypes;
    }

    public void setAllTypes(String newType) {
        allTypes.add(newType);
    }

    public List<String> getAllNames() {
        return allNames;
    }

    public void setAllNames(String newName) {
        allNames.add(newName);
    }

    public List<String> getAllSurfaces(){
        return allSurfaces;
    }
    public void setAllSurfaces(List<String> surfaces) {
        // Ugly but easy gymnastics to avoid duplicates
        HashSet<String> allSurfacesSet = new HashSet<>(this.allSurfaces);
        for (String surface : surfaces)
            allSurfacesSet.add(surface);
        List<String> allSurfaces = new ArrayList<>(allSurfacesSet);
        this.allSurfaces = allSurfaces;
    }

    public Map<String, Map<String, Unit>> getHospitalLayout() {
        return hospitalLayout;
    }

    public void setHospitalLayout(String unitType, String unitName, long first, long last, List<String> surfaces) {
        Map<String, Map<String, Unit>> hospitalLayout = new HashMap<>();
        Map<String, Unit> nameRoomsSurfaces = new HashMap<>();
        Unit roomsSurfaces = new Unit();
        roomsSurfaces.setUnit(first, last, surfaces);
        nameRoomsSurfaces.put(unitName, roomsSurfaces);
        hospitalLayout.put(unitType, nameRoomsSurfaces);
        this.hospitalLayout = hospitalLayout;

        setAllTypes(unitType);
        setAllNames(unitName);
        setAllSurfaces(surfaces);
    }

    public Map<String, Unit> getUnitNamesRoomsSurfaces(String type, Map<String, Map<String, Unit>> hospitalLayout){
        // Extracts the namesRoomsSurfaces from the HospitalLayout using the passed Type
        Map<String, Map<String, Unit>> layout = hospitalLayout;
        Map<String, Unit> namesRoomsSurfaces = layout.get(type);
        return namesRoomsSurfaces;
    }

    public Unit getRoomsSurfaces (String name, Map<String, Unit> nameRoomsSurfaces){
        return nameRoomsSurfaces.get(name);
    }
}

(Unit是第二类,在DBStructure HashMap中,在同一父键下同时具有整数和List值似乎是必需的)。
    public class Unit {
    // Defines the unit Name-Rooms-Surfaces
    Long firstRoom;
    Long lastRoom;
    List<String> surfaceList = new ArrayList<>();
    //Unit mUnit = new Unit();

    public Unit() {
    }


    public void setUnit (long firstRoom, long lastRoom, List<String> surfaceList) {
        this.firstRoom = firstRoom;
        this.lastRoom = lastRoom;
        this.surfaceList = surfaceList;
    }

    public Long getFirstRoom() {
        return firstRoom;
    }

    public Long getLastRoom() {
        return lastRoom;
    }

    public List<String> getSurfaceList() {
        return surfaceList;
    }

}

...最后,这是Firestore中数据的样子,它是由一个简单的.set(mDBStructure)编写的

java - 无法从Firestore读取自定义对象-为什么?-LMLPHP

我要感谢那些纯粹出于利他行为而花时间阅读此意大利面条式代码的人。

最佳答案

我认为您必须特别指出要反序列化的字段。
例如,在您的情况下,在onSuccess内

DocumentSnapshot document = documentSnapshot.getResult();
DbStructure db = new DbStructure();
db.setAllNames((List<String>) document.get("allNames"));
db.setAllTypes((List<String>) document.get("allTypes"));`

等等。在这种情况下,当反序列化返回的对象时,不会发生冲突。顺便说一句,在设置方法中,您必须将参数更改为“列表类型”)))。

我没有测试它,我希望它能工作))

09-16 00:14