Why does my app error when I am Getting data from firestore in class Android Studio

I am trying to get data from firestore in class but I got an error and I couldnt solve it.

I am trying to get some data from firestore inside GetWeeks class.


My GetWeeks class

    public class GetWeeks  {

    FirebaseFirestore mFirestore;
    String WeekContent;
    int WhichWeek = 1;
    String path[];
    Activity _activity;
    Context context;

    public GetWeeks(Context context){
        this.context = context;
        mFirestore = FirebaseFirestore.getInstance();
    }

    public void init()
    {

    }

    protected void GetWeekContent(String ders_adi) {

         mFirestore.collection("Lessons")
                .document(ders_adi)
                .collection("Documents")
                .document("Docs1")
                .get()
                .addOnSuccessListener(context, new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(@NonNull QuerySnapshot queryDocumentSnapshots) {

                    List<DocumentSnapshot>snapshotList= queryDocumentSnapshots.getDocuments();

                        for (DocumentSnapshot snapshot : snapshotList) {

                            WeekContent = snapshot.getString("Hafta" + WhichWeek);
                            path[WhichWeek] = WeekContent;

                           /* if(sifre != null && sifre.equals(sifre_from_teacherLogin) ){
                                model = new Model(ders_adi);
                                arraylist.add(model);
                            }*/

                        }


                    }
                }).addOnFailureListener(_activity, new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {

                    }
                });


    }
}

Error Message*

error: no suitable method found for addOnSuccessListener(Context,<anonymous OnSuccessListener<QuerySnapshot>>)
            .addOnSuccessListener(context, new OnSuccessListener<QuerySnapshot>() {
            ^
method Task.addOnSuccessListener(Executor,OnSuccessListener<? super DocumentSnapshot>) is not applicable
  (argument mismatch; Context cannot be converted to Executor)
method Task.addOnSuccessListener(Activity,OnSuccessListener<? super DocumentSnapshot>) is not applicable
  (argument mismatch; Context cannot be converted to Activity)

I tried to solve the error but I couldn’t solve it.Is it not possible to pull data from firestore in class?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Your code is getting a single document, so the success listener takes a DocumentSnapshot parameter instead of the QuerySnapshot you have now.

So:

 mFirestore.collection("Lessons")
    .document(ders_adi)
    .collection("Documents")
    .document("Docs1")
    .get()
    .addOnSuccessListener(context, new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(@NonNull DocumentSnapshot snapshot) {
            WeekContent = snapshot.getString("Hafta" + WhichWeek);
            path[WhichWeek] = WeekContent;
        }
        ...

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply