如何从Cloud Firestore获取阵列数据字段中的数据? [英] How can I get data from array data field from Cloud Firestore?

查看:174
本文介绍了如何从Cloud Firestore获取阵列数据字段中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cloud Firestore,并且字段中有一个数组数据。那么如何从数组中单独获取数据?

I am using Cloud Firestore and there is a array data in field. So how can I get data from array individually?

推荐答案

要获取 ip_range 数组中的值,请使用以下代码行:

To get the values within ip_range array, please use the following lines of code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
rootRef.collection("aic").document("ceo").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                Map<String, Object> map = document.getData();
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    if (entry.getKey().equals("ip_range")) {
                        Log.d("TAG", entry.getValue().toString());
                    }
                }
            }
        }
    }
});

另一种方法是:

rootRef.collection("products").document("06cRbnkO1yqyzOyKP570").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                ArrayList<String> list = (ArrayList<String>) document.get("ip_range");
                Log.d(TAG, list.toString());
            }
        }
    }
});

在这两种情况下,logcat中的输出将是:

In both cases, the output in your logcat will be:

[172.16.11.1, 172.16.11.2]

这篇关于如何从Cloud Firestore获取阵列数据字段中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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