将DocumentSnapshot数据转换为List

时间:2018-03-13 21:53:14

标签: android firebase google-cloud-firestore

我想从文档" list_of_clients"中读取每个对象列表。并将其导出到我创建的自定义列表

在实时数据库中有一个名为的函数 GenericTypeIndicator 这有助于你获得价值,这与我想要的相似,但在 Cloud FireStore 上没有这样的功能

我对Android很新,我不知道我能解释多少 但这里有一些照片:

这是我的数据库: mydb

这是我的user_list.java

private String user_addr;
private String user_name;
private String user_phone;


public user_list(String user_addr, String user_name, String user_phone) {
    this.user_addr = user_addr;
    this.user_name = user_name;
    this.user_phone = user_phone;
}

public user_list() {

}

public String getUser_phone() {
    return user_phone;
}

public void setUser_phone(String user_phone) {
    this.user_phone = user_phone;
}

public String getUser_name() {
    return user_name;
}

public void setUser_name(String user_name) {
    this.user_name = user_name;
}

public String getUser_addr() {
    return user_addr;
}

public void setUser_addr(String user_addr) {
    this.user_addr = user_addr;
}

我尝试了什么:

        db.collection("clients").document("list_of_clients")
            .get()
            .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        //if read successful

                        DocumentSnapshot document = task.getResult();

                        List<user_list> messages = document.getData();

                            Toast.makeText(getApplicationContext(), document.getId() + " => " + document.getData(), Toast.LENGTH_LONG).show();

                    } else {
                        //eroare
                    }
                }
            });

1 个答案:

答案 0 :(得分:1)

要使其正常工作,请参阅以下步骤。因此,为了正确反序列化字段,请考虑使用如下所示的模型类:

public class UserList {
    private String userAddr, userName, userPhone;

    public UserList() {}

    public UserList(String userAddr, String userName, String userPhone) {
        this.userAddr = userAddr;
        this.userName = userName;
        this.userPhone = userPhone;
    }

    public String getUserAddr() {return userAddr;}

    public String getUserName() {return userName;}

    public String getUserPhone() {return userPhone;}
}

查看命名约定? Firebase将始终关注看起来像userAddr的字段和看起来像getUserAddr()的公共getter。

要以正确的方式将UserList对象添加到Cloud Firestore数据库,请使用以下代码:

UserList userList = new UserList("Bolintin Vale", "Bogdan", "0251206260");
String docId = rootRef.collection("clients").document().getId();
rootRef.collection("clients").document(docId).set(userList);

要实际读取数据,请使用以下代码:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
rootRef.collection("clients").document("pu8NKFPNUKYgcy0yOitW").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot) {
        UserList userList = documentSnapshot.toObject(UserList.class);
        Log.d(TAG, userList.getUserName());
    }
});

正如您所看到的,我在代码中使用了之前生成的pu8NKFPNUKYgcy0yOitW id。输出将是:

Bogdan

BAFTA! ;)