I wanted to add new sub-Collection to a existing document. Should i create new POJO class to add Sub-Collection or any other way to do it. I wanted to add new Sub-Collection to existing document ID.I am new to android and Firestore. Thanks in advance. this is my Database
i tried this method but stuck couldn't succed
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();
}
});
}
There is no need to make something special. As I see in your code, you're already using the right reference:
CollectionReference NewCategory = NC
.collection("Categories")
.document("tUdFCajDcQT995jX6G4k")
.collection(category_name.getText().toString());
category_name.getText().toString()
being the name of the sub-collection but the problem rises in the following line of code:
NewCategory.add()
Where you aren't passing anything as an argument. You should pass a custom object or a Map in order to be able to write something in the database.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.