Android 如何在Firebase中执行成功方法?
我正在开发一个Android+Firebase应用程序,但由于我对这两种技术都不熟悉,我在异步调用方面遇到了问题,我还没有找到解决方案,希望您能帮助我 我的活动的Android 如何在Firebase中执行成功方法?,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我正在开发一个Android+Firebase应用程序,但由于我对这两种技术都不熟悉,我在异步调用方面遇到了问题,我还没有找到解决方案,希望您能帮助我 我的活动的onCreate方法中有以下代码片段: final ArrayList<AssetLocation> assetsLocations = new ArrayList<>(); DatabaseReference assetsLocationReference = database.getRefer
onCreate
方法中有以下代码片段:
final ArrayList<AssetLocation> assetsLocations = new ArrayList<>();
DatabaseReference assetsLocationReference = database.getReference(FirebaseReferences.ASSETSLOCATION_REFERENCE);
assetsLocationReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
ArrayList<String> assetsLocationSpinner = new ArrayList<String>();
// Create an ArrayAdapter using the string array and a default spinner layout
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
//Getting the data from snapshot
AssetLocation assetsLocation = postSnapshot.getValue(AssetLocation.class);
assetsLocations.add(AssetLocation);
}
}
@Override
public void onCancelled(DatabaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
final ArrayList assetsLocations=new ArrayList();
DatabaseReference AssetLocationReference=database.getReference(FirebaseReferences.AssetLocation\u引用);
AssetLocationReference.addValueEventListener(新的ValueEventListener(){
@凌驾
公共无效onDataChange(数据快照快照){
ArrayList AssetLocationSpinner=新建ArrayList();
//使用字符串数组和默认微调器布局创建ArrayAdapter
对于(DataSnapshot postSnapshot:snapshot.getChildren()){
//从快照获取数据
AssetLocation AssetLocation=postSnapshot.getValue(AssetLocation.class);
资产位置。添加(资产位置);
}
}
@凌驾
取消后的公共无效(DatabaseError firebaseError){
System.out.println(“读取失败:+firebaseError.getMessage());
}
});
我也有一个非常类似的电话,但不是得到位置,而是得到类型
在这段代码之后(也在onCreate方法内部),我将调用setScreenInfo
,这是一个用所述数据填充两个微调器(并执行更多操作)的函数,但由于它是一个异步调用,所以执行它时微调器是空的
调用后如何执行setScreenInfo
?我尝试了.once()/.on()
,但Android Studio没有将其识别为函数
谢谢你的帮助
在这段代码之后(也在onCreate方法内部),我将调用setScreenInfo,这是一个用上述数据填充两个微调器(并执行更多操作)的函数,但由于它是一个异步调用,所以在执行它时,微调器是空的
不允许您从后台线程修改UI。通常我会调用runOnUIThread
,传递一个新的Runnable()和一个我想要传递的数据的final
副本
final String myData = "updateData";
ActivityName.this.runOnUiThread(new Runnable() {
public void run() {
// use myData to update UI
}
});
但是,似乎有一个迁移到AsyncTask
:
我个人仍然使用
runOnUIThread
。它更明确。由于异步行为,您需要移动资产位置的声明
ArrayList:
final ArrayList<AssetLocation> assetsLocations = new ArrayList<>();
final ArrayList和这个
希望ot有帮助。您可以使用线程:
thread = new Thread() {
@Override
public void run() {
try {
//background tasks
runOnUiThread(new Runnable() {
@Override
public void run() {
//UI tasks
}
});
}
catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();`
也可以使用Firebase多查询方法。
链接: