java - Firebase 无法将检索到的数据保存到 ArrayList
问题描述
检索数据有效,但我无法将检索到的数据保存到 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;
}
解决方案
推荐阅读
- reactjs - React Native 显示/隐藏视图
- r - 为什么 R 中的 %in% 匹配不正确?
- json - 解析 json :测试是否存在 json 键
- java - 在初始化中向 Spring 定义的 FXMLController 注入 Spring 远程代理会引发 NoSuchBeanDefinitionException
- ffmpeg - 如何将这两个命令组合成一个命令?
- ios - Firebase 数据库读取 childByAutoId
- java - NotificationManager 错误 Android Studio
- r - 为什么 R 中的某些参数值的“外部”函数不能正常工作?
- ios - Apple 推送通知服务 SSL(沙盒和生产)证书是否通用?
- javascript - 如何在树节点 CSS 上显示全宽背景颜色