ArrayList<String> getthis=new ArrayList<String>();
getthis.add("username");
getthis.add("status");
*尝试达到目标-
方法将采用“用户名”,“状态”上方的形式,并从FirebaseDatabase获得其值作为新的类似数组,*
它这样做,它按照log表示检索值。 这些是检查应用程序时的日志。
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
在上面的日志之前,先向下面的日志扔
D/FirebaseHelper: At last FinalArray have size 0 and Element is []
Recived Array[]
但是最后,当main方法尝试返回Main Array时,则无法返回。
主要问题是-
Ide显示没有错误。但是方法无法返回由子方法构造的New Array。这是OnValueEventListner。
这些是主要代码
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;
}
}