将DocumentSnapshot数据转换为列表 [英] Convert DocumentSnapshot Data to List

查看:122
本文介绍了将DocumentSnapshot数据转换为列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to read each Object list from the document "list_of_clients" and export it to a custom List that i created

在实时数据库中,有一个名为 GenericTypeIndicator 这可以帮助您获取值,这与我想要的类似,但是在 Cloud FireStore 上没有此类功能

In Realtime Database there is a function called GenericTypeIndicator which helps you getting the values, this is similar to what i want but on Cloud FireStore there is no such function

我对android还是很陌生,我不知道我还能解释多少 但这里有一些照片:

I'm very new to android and i don't know how much more i can explain this but here are some pics:

这是我的数据库:

这是我的user_list.java

This is my 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
                    }
                }
            });

推荐答案

要使其正常运行,请参见以下步骤.因此,为了正确地反序列化字段,请考虑使用如下所示的模型类:

To make it work, please see the following steps. So, in order to deserialize the fields correctly, please consider using a model class that looks like this:

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()的公共获取器.

See the naming convention? Firebase will always look after fields that look like userAddr and a public getter that look like getUserAddr().

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

To add a UserList object in a correct way to your Cloud Firestore database, please use the following code:

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

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

To actually read the data, please use the following code:

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.输出将是:

As you can see, I have used in my code the pu8NKFPNUKYgcy0yOitW id that was generated before. The output will be:

Bogdan

巴法塔! ;)

这篇关于将DocumentSnapshot数据转换为列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆