如何在Android的Firestore文档中添加新的子集合

问题描述 投票:0回答:1

我想将新的子集合添加到现有文档中。我应该创建新的POJO类来添加Sub-Collection还是任何其他方式。我想向现有文档ID添加新的子集合。我是android和Firestore的新手。预先感谢。this is my Database

我尝试了此方法,但无法成功卡住

private void setNewCategory(String downloadUrl){
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    DocumentReference newMainCatRef = db
            .collection("HomeFeed")
            .document("5HEkE0ac7sMa7Gjnvf3E")
            .collection("MainCategory")
            .document();
    itemId = newMainCatRef.getId();
    MainCategory category = new MainCategory();

    category.setCategory_id(itemId);
    category.setCategory_name(category_name.getText().toString());
    category.setCategory_url(downloadUrl);
    category.setPriority(priority.getValue());

    newMainCatRef.set(category).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Log.d(TAG, "onSuccess: Success on Updating the new Field to cat");
            FirebaseFirestore NC = FirebaseFirestore.getInstance();
            CollectionReference NewCategory = NC
                    .collection("Categories")
                    .document("tUdFCajDcQT995jX6G4k")
                    .collection(category_name.getText().toString());
            NewCategory.add()

            category_name.setText("");
            priority.setValue(0);
            category_image.setImageResource(R.drawable.ic_android);
            category_progress.setVisibility(View.INVISIBLE);

        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(mContext, "Failed to add New Catgory Try again!", Toast.LENGTH_SHORT).show();
        }
    });

}
android firebase google-cloud-firestore
1个回答
0
投票

没有必要做一些特别的事情。正如我在您的代码中看到的那样,您已经在使用正确的参考:

CollectionReference NewCategory = NC
                .collection("Categories")
                .document("tUdFCajDcQT995jX6G4k")
                .collection(category_name.getText().toString());

category_name.getText().toString()是子集合的名称,但是在下面的代码行中出现了问题:

NewCategory.add()

您不传递任何参数的地方。您应该传递一个自定义对象或一个Map,以便能够在数据库中写一些东西。

© www.soinside.com 2019 - 2024. All rights reserved.