将FireStore集合映射到Java POJO

时间:2017-10-16 21:37:21

标签: android google-cloud-firestore

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

This is my database structure

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

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以轻松地将try { $.ajax({ url: 'myurl', dataType: 'jsonp', jsonp: 'jsoncallback', timeout: 5000, success: function(data, status){ $.each(data, function(i,item){ var res = '<h1>'+item.name+'</h1>' + '<p>'+item.value1+'<br>' + item.value2+'</p>'; output.append(res); }); }, error: function(){ output.text('There was an error loading the data.'); } }); } catch (e) { logMyErrors(e); } 映射到对象列表,假设您的应用包含与数据匹配的QuerySnapshot POJO类:

Hospital

医院课程可能如下所示:

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());
                }
            }
        });

参考: https://firebase.google.com/docs/firestore/reference/android/QuerySnapshot.html#toObjects(java.lang.Class