0

I'm having an issue. I have a variable I made called phone, which is an empty string. I then made a method to get the phone number from firestore and it worked. Now I am trying to use that value in my onCreate but it returns as an empty string.

any idea how I can resolve this?

here is my code


public class ReceiptsFragment extends Fragment {

    public RecyclerView recyclerView;
    public RecyclerView.LayoutManager layoutManager;

    private FirebaseFirestore db;
    private FirebaseAuth fAuth;

    private OrderAdapter adapter;
    public String phone = "";


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_receipts, container, false);



        //firebase
        fAuth = FirebaseAuth.getInstance();
        db = FirebaseFirestore.getInstance();


        getPhonenumber();

        Log.d("Phonenumber", "THE PHONE IS: "+ phone);
        Query requests = db.collection("Requests").whereEqualTo("phone", phone);

        FirestoreRecyclerOptions<Request> options = new FirestoreRecyclerOptions.Builder<Request>().
                setQuery(requests, Request.class).build();

        adapter = new OrderAdapter(options);

        recyclerView = (RecyclerView) view.findViewById(R.id.listOrders);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        return view;
    }

    public  void getPhonenumber() {
        final DocumentReference documentReference = db.collection("users").document(fAuth.getUid());
        documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                User user = documentSnapshot.toObject(User.class);
                phone = (String) documentSnapshot.get("phoneNumber");
                Log.d("Phonenumber", "THE PHONE IS ALSO : "+ phone);

            }
        });

    }

    @Override
    public void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

Here is the log I get:

2020-06-22 14:49:09.155 2942-2942/com.example.hostapp D/Phonenumber: THE PHONE IS: 
2020-06-22 14:49:09.454 2942-2942/com.example.hostapp D/Phonenumber: THE PHONE IS ALSO : 0634224297
Emil C.
  • 101
  • 1
  • 11
  • Firebase API is asynchronous. So please check the duplicate to how can you solve this using a custom callback. – Alex Mamo Jun 22 '20 at 13:15
  • hi @AlexMamo i followed the answer you posted and video. and called the:readData(new FirestoreCallback() inside my oncreate. how do i then update my variable, its still running with a blank string, ie my oncreate still runs before the callback, if i try to move my query and code inside the readData() my app crashes – Emil C. Jun 22 '20 at 20:25
  • All you need to do is add the code inside the callback or called from there. – Alex Mamo Jun 23 '20 at 06:30
  • @AlexMamo if you look at where i say //firebase, when i move all of that into the call back that is inside the onCreate, the whole app starts to crush and my query start getting null objects – Emil C. Jun 23 '20 at 09:39
  • 1
    i managed to fix it, thank you very much @AlexMamo – Emil C. Jun 23 '20 at 11:13

0 Answers0