猿问
如何从 Cloud Firestore 的数组数据字段中获取数据?
我正在使用 Cloud Firestore,并且字段中有一个数组数据。那么如何单独从数组中获取数据呢?
慕码人2483693
浏览 118
回答 1
1回答
慕容708150
要获取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]
0
0
0
随时随地看视频
慕课网APP
相关分类
Java
我要回答