1

I have seen several answers related to this but could not figure out how to do this from one week. ARRAYLIST IS empty I tried using a query, using the function but couldn't get that any easy or better WORKING solution for this

mUserDatabase=FirebaseDatabase.getInstance().getReference().child(uid).child("Medicines");

mUserDatabase.addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {

        for (DataSnapshot childDataSnapshot2 : childDataSnapshot.getChildren()) { 
              //GET MEDICINE NAME
               arrayList.add(childDataSnapshot2.getKey());

            }}

    @Override
    public void onCancelled(DatabaseError databaseError) {}});

enter image description here

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • First Try to debug that your dataSnapshot.exist() or not. If yes then track how many children it has! If you are getting all these true then your arrayList must be String type and you will get a bunch of values inside! If you are on the right track upto now and still not getting it, Then please share your database hierarchy – Pushpendra Dec 29 '19 at 12:16
  • I used Toast below that and it toast is working fine but slow. –  Dec 29 '19 at 12:18
  • but inside arraylist it is empty or size is 0 –  Dec 29 '19 at 12:18
  • Thanks @AlexMamo and Pushpendra I added that –  Dec 29 '19 at 12:22
  • 1
    @Rohitgupta You are likely trying to use the result of an *asynchronous* task before waiting for it to complete. When you say "toast is working fine but slow", this is because your client has to authenticate, query the database *for all of the data under `/someUid/Medicines`* and parse the results all before that Toast is shown. You should be showing a [Throbber](https://en.wikipedia.org/wiki/Throbber) while you wait for your data to be queried. – samthecodingman Dec 29 '19 at 14:05
  • As others have commented: you seem to be trying to use `arrayList` outside of `onDataChange`, which isn't possible because that code executes before the `onDataChange` has been called. Because data is loaded from Firebase asynchronously, any code that needs the data myst must be inside `onDataChange` or called from there. Also see my answer with examples here: https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Dec 29 '19 at 15:05

1 Answers1

2

To get the key and values from your database schema, please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference medicinesRef = rootRef.child(uid).child("Medicines");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String key = ds.getKey();
            String altisgpt = ds.child("ALTISGPT").getValue(String.class);
            Log.d(TAG, key + " -> " + altisgpt);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
medicinesRef.addListenerForSingleValueEvent(valueEventListener);

The output in your logcat will be:

2019-1-27 -> 72 u/L

In the same way, you can also get the value of the other properties. Besides that, you can create a Medicine (POJO) class using the same properties to map each node under Medicines to an object of type Medicine. In that way, you can simply create a list of Medicine objects and use it as needed. Please also don't forget to check my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I made String key as global then tried to use toast outside Toast.makeText(this, key, Toast.LENGTH_SHORT).show(); BUT TOAST was "" BLANK –  Dec 29 '19 at 13:02
  • @Rohitgupta In your example, `key` is being "toasted" before it has ever been set a value. You aren't waiting for the data to be queried and parsed. – samthecodingman Dec 29 '19 at 14:06
  • @Rohitgupta samthecodingman is right. That's the reason I gave you that example at the end of my answer. Have you solved the issue now? – Alex Mamo Dec 29 '19 at 14:49