mardi 24 mars 2020

How to use firebase query in if else condition based on user information

I am trying to create a query with an if/else statement condition where based on student faculty,programme and semester. Example if student studied in Faculty of Computing and Creative Technology and takes Bachelor of Computer Sciences (Hons) programme and currently in semester2/year1.How to retrieve all the courses in semester2/year1 for student courses enrollment?

Here is the picture of database Firebase Database

And here is the code for retrieve the courseID and CourseName based on student faculty,programme and semester.(actually i don't know how to do it).

im not showing all of my coding because i just show what the problem is.I hope u understand what im trying to tell.Thank you

```dbCourse = FirebaseDatabase.getInstance().getReference().child("Faculty");

    if(tvProgramme.equals("Bachelor Of Computer Sciences (Hons)")){
        if(tvSemester.equals("semester1/year1")){
            final Query query = FirebaseDatabase.getInstance().getReference("Faculty").child("Faculty of Computing and Creative Technology").child("Programme")
            .child("Bachelor of Computer Sciences (Hons)").child("Year 1").child("Semester 1");

            query.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    courseList= new ArrayList<CourseModel>();
                    for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
                    {
                        CourseModel p = dataSnapshot1.getValue(CourseModel.class);
                        courseList.add(p);

                    }
                    adapter = new RegisterCourseAdapter(courseList);
                    recyclerView.setAdapter(adapter);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Toast.makeText(RegisterCourseActivity.this, "There is an error occur. Please try again.", Toast.LENGTH_LONG).show();

                }
            });

        }
        else if(tvSemester.equals("semester2/year1")){
            final Query query = FirebaseDatabase.getInstance().getReference("Faculty").child("Faculty of Computing and Creative Technology").child("Programme")
                    .child("Bachelor of Computer Sciences (Hons)").child("Year 1").child("Semester 2");

                query.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        courseList= new ArrayList<CourseModel>();
                        for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
                        {
                            CourseModel p = dataSnapshot1.getValue(CourseModel.class);
                            courseList.add(p);

                        }
                        adapter = new RegisterCourseAdapter(courseList);
                        recyclerView.setAdapter(adapter);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Toast.makeText(RegisterCourseActivity.this, "There is an error occur. Please try again.", Toast.LENGTH_LONG).show();

                    }
                });


        }
        else if(tvSemester.equals("semester3/year1")){
            final Query query = FirebaseDatabase.getInstance().getReference("Faculty").child("Faculty of Computing and Creative Technology").child("Programme")
                    .child("Bachelor of Computer Sciences (Hons)").child("Year 1").child("Semester 3");

            query.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    courseList= new ArrayList<CourseModel>();
                    for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
                    {
                        CourseModel p = dataSnapshot1.getValue(CourseModel.class);
                        courseList.add(p);

                    }
                    adapter = new RegisterCourseAdapter(courseList);
                    recyclerView.setAdapter(adapter);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Toast.makeText(RegisterCourseActivity.this, "There is an error occur. Please try again.", Toast.LENGTH_LONG).show();

                }
            });


        }


    }```

Aucun commentaire:

Enregistrer un commentaire