我正在嘗試在 android studio 中使用 Firestore 制作課程添加系統。
當用戶添加課程時,它將被添加到 Firestore,并且該課程集合中將有一個學生和教師集合。
但我不知道該怎么做。
如果我為學生和教師集合撰寫代碼,它會添加兩個不同的集合,但我希望在一個課程集合中有 2 個集合,包括學生和教師。
我怎樣才能做到這一點?
我分享我寫的代碼。
添加課程
private ActivityDersEkleBinding binding;
private FirebaseFirestore mFirestore;
private String txt_lesson;
private LinkedHashMap<String,String> linkedHashMap;
private int i=1;
private CollectionReference Courses;
public void init(){
linkedHashMap = new LinkedHashMap<>();
mFirestore = FirebaseFirestore.getInstance();
Courses = mFirestore.collection("Courses");
Ders_EKLE();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityDersEkleBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
init();
}
private void Ders_EKLE(){
//Ders ekleme tam olmad?. Tekrar bak?lacak.
binding.btnEkleDers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txt_lesson = binding.isimDers.getText().toString();
//Show alertdialog if the user has entered an empty lesson
if(txt_ders.equals("")){
AlertDialog.Builder builder = new AlertDialog.Builder(DersEkleActivity.this);
builder.setTitle("WARNING!");
builder.setMessage("you cant empty value !");
builder.setIcon(R.drawable.warningicon);
builder.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
}
else {
//If a lesson has been entered, add it to the firestore.
linkedHashMap.put("lesson" i,txt_lesson);
int j=1;
Courses.document("KDXnJKKno1D2xtG9G6UE").collection("Lessons")
.document()
.collection(txt_lesson)
.document()
.collection("Student")
.add(new DersEklePerson())
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(@NonNull DocumentReference documentReference) {
Toast.makeText(DersEkleActivity.this,"Ders ba?ar?yla eklendi.",Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
}
});
}
編輯

uj5u.com熱心網友回復:
Firestore 資料模型是:
- 在頂層,您有集合。
- 在每個集合中,您(可以)擁有檔案。
- 每個檔案都可以再次嵌套集合。
- 等等。
所以一個集合既可以存在于根級別,也可以存在于檔案下。您不能在集合中直接擁有一個集合,因為您在問題中反復說想要。
這也是最常見的有象征意義的名字,你可以在你的代碼指定的集合,如users,courses,lessons,questions,等等,那么你可以生成檔案的ID內的集合。
您的資料模型似乎沒有遵循這些常見模式,我認為這是您遇到問題的部分原因。
有了上面的知識,我們來看看這段代碼:
Courses.document("KDXnJKKno1D2xtG9G6UE").collection("Lessons")
.document()
.collection(txt_lesson)
.document()
.collection("Student")
.add(new DersEklePerson())
開始很好:
Courses聽起來像一個集合的名稱,并且你在那里的檔案 ID 看起來是生成的,所以 ??子集
Lessons看起來也不錯。??但是當你這樣呼叫時
document(),它會生成一個新的檔案參考,指向一個新的、不存在的檔案,這似乎不太可能是你想要的。您更有可能需要由 標識的課程檔案,
txt_lesson您可以使用document(txt_lesson).如果你這樣做,你也不再需要第二個
document()電話了。然后你有一個
Student子集合,它看起來又好了。??好吧,我建議稱它為
Students,因為您的其他集合名稱也是復數形式。最后,您呼叫
add()該Student集合為學生生成檔案。??
所以畢竟,我認為你需要:
Courses.document("KDXnJKKno1D2xtG9G6UE").collection("Lessons")
.document(txt_lesson)
.collection("Student")
.add(new DersEklePerson())
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/374471.html
標籤:爪哇 安卓 火力基地 谷歌云firestore
上一篇:變數替換無法將--exclude選項傳遞給tar命令
下一篇:Microsoft.Data.SqlClientNuGet在某些專案中是需要的,但在其他專案中您可以使用Microsoft.Data.SqlClient而不使用NuGet
