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

Time:02-16

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?

CodePudding user response:

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;
        }
        ...
  • Related