Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Firebase can';t将检索到的数据保存到ArrayList_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java Firebase can';t将检索到的数据保存到ArrayList

Java Firebase can';t将检索到的数据保存到ArrayList,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,检索数据可以工作,但我无法将检索到的数据保存到ArrayList中。紧跟在“onDataChanged()”方法之后的ArrayList“profile”似乎有2个值,但在return语句中有0 static List<Profile> profiles = new ArrayList<Profile>(); static DatabaseReference dbr; public static List<Profile> loadProfiles(Cont

检索数据可以工作,但我无法将检索到的数据保存到ArrayList中。紧跟在“onDataChanged()”方法之后的ArrayList“profile”似乎有2个值,但在return语句中有0

static List<Profile> profiles = new ArrayList<Profile>();
static DatabaseReference dbr;

public static List<Profile> loadProfiles(Context context){

    dbr = FirebaseDatabase.getInstance().getReference().child("users").child("hiring");
    dbr.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            //String value = dataSnapshot.getValue(String.class);
            //Log.d("hello", "Value is: " + value);
            List<Profile> profiles2 = new ArrayList<>();

                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Profile profile = snapshot.getValue(Profile.class);
                    //Log.d("hello", profile.getCompanyName());
                    profiles2.add(profile);

                }

                profiles = profiles2;
                dbr.removeEventListener(this);
        }




        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w("hello", "Failed to read value.", error.toException());
        }
    });

    return profiles;

}
static List profiles=new ArrayList();
静态数据库参考数据库;
公共静态列表加载配置文件(上下文){
dbr=FirebaseDatabase.getInstance().getReference().child(“用户”).child(“雇用”);
dbr.addListenerForSingleValueEvent(新的ValueEventListener()){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
//使用初始值调用此方法一次,然后再次调用
//每当更新此位置的数据时。
//String value=dataSnapshot.getValue(String.class);
//Log.d(“你好”,“值为:”+Value);
List profiles2=new ArrayList();
对于(DataSnapshot快照:DataSnapshot.getChildren()){
Profile Profile=snapshot.getValue(Profile.class);
//Log.d(“hello”,profile.getCompanyName());
profiles2.添加(profile);
}
profiles=profiles2;
dbr.removeEventListener(本文件);
}
@凌驾
已取消的公共无效(DatabaseError错误){
//无法读取值
Log.w(“hello”,“读取值失败”,error.toException());
}
});
返回配置文件;
}

您现在无法返回尚未加载的内容。换句话说,您不能简单地在
onDataChange()
方法之外返回
profiles
列表,因为由于此方法的异步行为,它将始终为
空。这意味着,当您试图在该方法之外返回结果时,数据尚未从数据库中加载完毕,因此无法访问

这个问题的一个快速解决方案是只在
onDataChange()
方法中使用
profiles
列表,否则我建议您查看我的回答的最后一部分,我在其中解释了如何使用自定义回调来完成。您还可以查看此,以便更好地理解

编辑:2021年2月26日

有关更多信息,您可以查看以下文章:

以及以下短片:


    • 您现在无法返回尚未加载的内容。换句话说,您不能简单地在
      onDataChange()
      方法之外返回
      profiles
      列表,因为由于此方法的异步行为,它将始终为
      空。这意味着,当您试图在该方法之外返回结果时,数据尚未从数据库中加载完毕,因此无法访问

      这个问题的一个快速解决方案是只在
      onDataChange()
      方法中使用
      profiles
      列表,否则我建议您查看我的回答的最后一部分,我在其中解释了如何使用自定义回调来完成。您还可以查看此,以便更好地理解

      编辑:2021年2月26日

      有关更多信息,您可以查看以下文章:

      以及以下短片:


      这是因为addListenerForSingleValueEvent是异步的。这是因为addListenerForSingleValueEvent是异步的。是的,我通过对“onDataChange()”中的数据做我需要做的一切来解决问题。谢谢。是的,我通过对“onDataChange()中的数据做我需要做的一切来解决问题非常感谢。