どのように私はクラウドFirestoreからの配列データフィールドからデータを得ることができますか?

アービンド・クマール:

私はクラウドFirestoreを使用していますが、フィールドの配列データがあります。だから、どのように私は、個々の配列からデータを得ることができますか?

ここでは、画像の説明を入力します。

アレックス・マモ:

内の値を取得するにはip_range、アレイ、次のコード行を使用してください:

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出力は次のようになります。

[172.16.11.1, 172.16.11.2]

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=203416&siteId=1