I'm trying to make a lesson add system using Firestore in android studio.
When the user adds a lesson, it will be added to Firestore and there will be a student and teacher collection in that lesson collection.
But I don't know how to do it.
If I write code for student and teacher collections, it adds two different collections, but I want to have 2 collections, both student and teacher, in one lesson collection.
How can I do this?
I share the code that I wrote.
add lesson class
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) {
}
});
}
}
});
}
CodePudding user response:
The Firestore data model is:
- At the top level you have collections.
- In each collection you (can) have documents.
- Each each document you can have nested collections again.
- Etc.
So a collection can either exist at the root level, or under a document. You cannot have a collection directly inside a collection, as you repeatedly say you want in your question.
It is also most common to have symbolic names that you can specify in your code for collections such as users, courses, lessons, questions, etc. Then you can generate the IDs for the documents inside those collection.
You data model seems to not follow these common patterns, which I think is part of the reason you are running into problems.
Let's have a look at this code, with the knowledge above:
Courses.document("KDXnJKKno1D2xtG9G6UE").collection("Lessons")
.document()
.collection(txt_lesson)
.document()
.collection("Student")
.add(new DersEklePerson())
It starts good:
Coursessounds like the name of a collection, and the document ID you have in there looks generated, so

