【问题标题】:How can I add two collection into collection android studio in firestore如何将两个集合添加到 Firestore 中的集合 android studio
【发布时间】:2022-01-11 18:52:07
【问题描述】:

我正在尝试在 android studio 中使用 Firestore 制作课程添加系统。

当用户添加课程时,该课程将被添加到 Firestore,并且该课程集合中将有一个学生和教师集合。

但我不知道该怎么做。

如果我为学生和教师集合编写代码,它会添加两个不同的集合,但我希望在一个课程集合中包含两个集合,学生和教师。

我该怎么做?

我分享我写的代码。

添加课程

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) {

                    }
                });





            }

        }
    });

}

编辑

【问题讨论】:

    标签: java android firebase google-cloud-firestore


    【解决方案1】:

    Firestore 数据模型是:

    1. 在顶层您有收藏。
    2. 在每个集合中,您(可以)有文档。
    3. 每个文档都可以再次嵌套集合。

    所以集合可以存在于根级别,也可以存在于文档下。正如您在问题中反复说的那样,您不能将集合直接放在集合中。


    最常见的还有可以在代码中为集合指定的符号名称,例如userscourseslessonsquestions 等。然后您可以为文档生成 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,因为您的其他收藏名称也是复数。

    • 最后,您在该 Student 集合上调用 add(),为学生生成一个文档。 ?

    所以,毕竟,我认为你需要:

    Courses.document("KDXnJKKno1D2xtG9G6UE").collection("Lessons")
            .document(txt_lesson)
            .collection("Student")
            .add(new DersEklePerson())
    

    【讨论】:

    • 谢谢你的回答,它变得更加简单和好,但我也想在学生收藏下有老师收藏。
    • 我在上面分享了一张图片,你可以在那里看到
    • 我该怎么做?
    • 类似于Courses.document("KDXnJKKno1D2xtG9G6UE").collection("Lessons").document(txt_lesson).collection("Teaches").add(...)
    • 非常感谢。它起作用了:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 2019-02-08
    • 2019-06-04
    • 2019-04-28
    • 1970-01-01
    相关资源
    最近更新 更多