0

I apply (Callback method to fix the asynchronous problem)the https://www.youtube.com/watch?v=0ofkvm97i0s video. But I have problems.

Hear are readData and interface.

    private void readData(FirestoreCallback firestorecallback){
    String currentUserId = firebaseAuth.getCurrentUser().getUid();
    DocumentReference docRef = firestore.collection("Users").document(currentUserId);
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if(task.isSuccessful()){
                DocumentSnapshot document = task.getResult();
                if(document.exists()){
                    a = document.getString('gender');
                }
                firestorecallback.onCallback(a);
            }

        }
    });
}
private interface FirestoreCallback{
    void onCallback(String b);
}

And in onCreate method

readData(new FirestoreCallback() {
        @Override
        public void onCallback(String b) {
            String c;
            if(b=='남자'){
                c ='여자';
            }
            if(b=='여자'){
                c = '남자';
            }
            EventChangeListener(b);
            Log.d('',b);
            Log.d('',c);
        }
    });

EventChangeListener function's code

private void EventChangeListener(String a) {

    //firestore.collection('Users').orderBy('time',Query.Direction.DESCENDING).limit(50)
    // 
    firestore.collection('Users')
            //.whereEqualTo('mbti','ENTP')
            //.whereEqualTo('region','')
            .whereEqualTo('gender',a)
            .orderBy('time',Query.Direction.DESCENDING).limit(50)
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
                    if(error != null){

                        if(progressDialog.isShowing()){
                            progressDialog.dismiss();
                        }

                        Log.e("Firestore error",error.getMessage());
                        return;

                    }

                    for (DocumentChange dc : value.getDocumentChanges()){

                        if (dc.getType() == DocumentChange.Type.ADDED){
                            userArrayList.add(dc.getDocument().toObject(User.class));
                        }

                        myAdapter.notifyDataSetChanged();
                        if(progressDialog.isShowing()){
                            progressDialog.dismiss();
                        }

                    }

                }
            });
}

As you can see, I can get the variable 'b' but I want to when users' 'b' is male c-> male, 'b' is female c-> male. So I define the variable 'c' but android said variable c is not defined

public void onCallback(String b) {
        String c;
        if(b=='female'){
            c ='male';
        }
        if(b=='male'){
            c = 'female';
        }
        EventChangeListener(b);
        Log.d('',b);
        Log.d('',c);
    }
Eye Lamp
  • 69
  • 5
  • 1
    Have you created the corresponding [index](https://stackoverflow.com/questions/50305328/firestore-whereequalto-orderby-and-limit1-not-working)? – Alex Mamo Mar 02 '22 at 09:40
  • @AlexMamo Thank you!!, But why firebase didn't create the index automatically? – Eye Lamp Mar 08 '22 at 12:21
  • @AlexMamo I created index but, still shows "variable c might not have been initialized" – Eye Lamp Mar 08 '22 at 12:32
  • @AlexMamo I think onCallback method can't allow the if statement.. but why? – Eye Lamp Mar 08 '22 at 12:38
  • Firebase creates indexes automatically but not for such queries. "variable c might not have been initialized" doesn't have something to do with indexes. The allows but be sure to use the right variable at the right time. – Alex Mamo Mar 08 '22 at 13:01
  • @AlexMamo Can you tell me about why if statement can't work. – Eye Lamp Mar 09 '22 at 04:40
  • What is the exact error that you have? Show us the error message or at least a screenshot of it. – Alex Mamo Mar 09 '22 at 07:34

0 Answers0