1
ArrayList<String> getthis=new ArrayList<String>();
getthis.add("username");
getthis.add("status");

*Trying to achive-

method will take above "username","status" and get their value from FirebaseDatabase as new simmilar array,*

And it do so,it retrives the value as log says. these are the Logs while checking app.

D/FirebaseHelper: Recived name=a@gmail.com
FinalArray have size 1 and Element is a@gmail.com
D/FirebaseHelper: Recived status=hi there buddy 
FinalArray have size 2 and Element is hi there buddy 

Ide throw below Log before above Logs

D/FirebaseHelper: At last FinalArray have size 0 and Element is []
Recived Array[]

But at the end ,when main method try to return Main Array-then it can't.

Main problem is-

Ide shows no error.but method is unable to return New Array constructed by child method.Which is OnValueEventListner.

these are the main codes

package com.abhishekwork.forwork;

import android.util.Log;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

import androidx.annotation.NonNull;

public class HardWork {

    private static String tempString;


    public static ArrayList<String> getArrayFromFirebase(DatabaseReference databaseReference, final ArrayList<String> arrayList) {

        final int inputArraySize = arrayList.size();
        final ArrayList<String> finalArrayList = new ArrayList<>(inputArraySize);


        databaseReference.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (int i = 0; i < inputArraySize; i++) {
                    tempString = dataSnapshot.child(arrayList.get(i)).getValue().toString();
                    Log.d("FirebaseHelper","Recived"+arrayList.get(i) + "=" + tempString);

                    finalArrayList.add(i,Temp_String);
                    Log.d("FirebaseHelper","FinalArray has size" + finalArrayList.size() + " and Element is " + finalArrayList.get(i));
                }
            }

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

        Log.d("FirebaseHelper", "At last FinalArray have size" + finalArrayList.size() + "and Element is " + finalArrayList.toString());

        Log.d("FirebaseHelper", "Recieved Array" + finalArrayList);

        return finalArrayList;
    }


}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

When you call addValueEventListener() on the DatabaseReference, you are adding a listener that will have it's onDataChange() method fired when the data snapshot is ready (or the onCancelled() will be fired if there was an error).
These events are asynchronous and they probably happen only after you return your from your method and that's why you are getting the empty list.

The simplest way to get the results you need would be using a callback to do work when the results are ready.
Here's a great answer which you can refer to.

  • yah,i visited the link you provided,but still i am confused ,how to do this with my code,am trying to go with your suggestion. – abhishek rana Dec 07 '18 at 16:26
  • Thanks #Mikhail Olshanski ,Your answer was very helpful.This took a little time to me because before this i didn't know about this approach. – abhishek rana Dec 07 '18 at 19:14