亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法將 firestore 中的數據值存儲到數組中?

有沒有辦法將 firestore 中的數據值存儲到數組中?

元芳怎么了 2024-01-05 15:18:32
我試圖獲取每個文檔中兩個特定字段中的數據,其中一個字段是整數,另一個字段是字符串。我希望每個字段分別存儲在一個數組中。 db.collection("Activity")                .whereEqualTo("plan_id", plan_id)                .get()                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {                    @Override                    public void onComplete(@NonNull Task<QuerySnapshot> task) {                        if (task.isSuccessful()) {                            List<Long> progressList = new ArrayList<>();                            List<String> titleList = new ArrayList<>();                            for (QueryDocumentSnapshot document : task.getResult()) {                                Log.d(TAG, document.getId() + " => " + document.get("progress"));                            }                        } else {                            Log.d(TAG, "Error getting documents: ", task.getException());                        }                    }                });我試過了int[] prog = (int[]) document.get("progress");String[] title = (String[]) document.get("title");但沒有運氣...
查看完整描述

1 回答

?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

我對 Firestore 不熟悉,但似乎QueryDocumentSnapshot與Map<String, Object>. 以下代碼片段顯示了如何將這些值分別存儲到List代碼中聲明的progressList 和titleList 中。


List<int> progressList = new ArrayList<>();

List<String> titleList = new ArrayList<>();

for (QueryDocumentSnapshot document : task.getResult()) {

    progressList.add(Integer.valueOf(document.get("progress").toString()));

    titleList.add(document.get("title").toString());

}

如果你仍然想用來Array存儲值,你可以使用 API,ArrayList.toArray()如下所示:


int[] prog = new int[progressList.size()];

prog = progressList.toArray(prog);


String[] title = new String[titleList.size()];

title = titleList.toArray(title);


查看完整回答
反對 回復 2024-01-05
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號