// 创建数据库索引变量db
db = FirebaseFirestore.getInstance();
// 创建数据collection命名为CollectionName,添加索引索引变量
final CollectionReference collectionReference = db.collection("CollectionName");
// 创建哈希变量,准备数据用于doc的property填充
HashMap<String, String> data = new HashMap<>();
data.put("KeyName", valueName);
// 将数据更新到collectionReference:
// 将docName作为doc名,哈希表包含各键值对作为参数,设定添加状态监听
collectionReference
.document(docName)
.set(data)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// These are a method which gets executed when the task is succeeded
Log.d(TAG, "Data has been added successfully!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// These are a method which gets executed if there’s any problem
Log.d(TAG, "Data could not be added!" + e.toString());
}
});
[Firebase] 创建Collection & Document (in Java)
于 2022-02-11 12:42:57 首次发布