0

String returning null value in android studio (java). I want to store the value from onComplete method to a string and use that outside that onComplete method. Please help me with the same..

final String[] sex = new String[1];
        DocumentReference documentReference = firebaseFirestore.collection("users").document(mFirebaseAuth.getCurrentUser().getUid());
        documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {

            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    // Document found in the offline cache
                    DocumentSnapshot document = task.getResult();
                    sex[0] = document.getString("sex");
                    Log.d("", "Cached document data: " + document.getData());
                } else {
                    Log.d("TAG", "Cached get failed: ", task.getException());
                }
            }
        });
        Toast.makeText(getContext(), "Gender :" + sex[0], Toast.LENGTH_SHORT).show();

        Query query = null;
        if (sex[0] == "Male") {
            query = firebaseFirestore.collection("Female");
        } else if (sex[0] == "Female") {
            query = firebaseFirestore.collection("Male");
        } else {
            query = firebaseFirestore.collection("Others");
        }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
robin
  • 9
  • 4
  • You can do that but then you only can use that string after onComplete() has run. I think that you now use the string too early as i dont see you waiting for onComplete() finishing. – blackapps Aug 29 '21 at 17:46
  • Put the code you would use to handle that string in a function. And call that function in onComplete. – blackapps Aug 29 '21 at 17:48
  • I have to use that string after the onComplete function only as I have to use that value for my recylcerview adapter. and when i am calling this inside the function its giving me error in onStrat method as there i am using adapter.startlistening() method. help me with this – robin Aug 29 '21 at 17:57
  • You are asking too much without me knowing what you do. Sorry. – blackapps Aug 29 '21 at 18:01
  • 1
    Data is loaded from Firebase (and most cloud APIs) asynchronously. While that is happening, you main code continues and your toast shows. Then once the data is loaded, your `onComplete` is called, and sets `sex[0]`. So: your toast is shown before the data is loaded. The solution is always the same: any code that needs the data from the database, needs to be inside the `onComplete` or be called from there. I've linked a few previous questions on the topic. – Frank van Puffelen Aug 29 '21 at 18:03
  • So if you need the data in your adapter, you'll have to initialize that adapter (or at least update its data structure) in the `onComplete` method. – Frank van Puffelen Aug 29 '21 at 18:06
  • Doing so, is giving me error in onStart method where I'm calling adapter.startlistening()...how to avoid that. – robin Aug 29 '21 at 18:20

0 Answers0