有什么方法可以从Firebase实时数据库中获取后台数据吗? Asynctask
不起作用。
我想在从数据库中获取数据时显示进度对话框。
private class asyncData extends AsyncTask<String,String,String> {
ProgressDialog Asycdialog = new ProgressDialog(getContext());
@Override
protected void onPreExecute() {
super.onPreExecute();
Asycdialog.setMessage("Loading...");
Asycdialog.show();
}
@Override
protected String doInBackground(String... strings) {
Query query = databaseReference.child("").orderByChild("cityWithCountry").equalTo(strings[0]);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
arrData.clear();
if (dataSnapshot.exists()) {
// dataSnapshot is the "issue" node with all children with id 0
for (DataSnapshot items : dataSnapshot.getChildren()) {
// do something with the individual "issues"
Log.d("QueryValues",""+items.getValue());
DataConstructor ds = items.getValue(DataConstructor.class);
arrData.add(ds);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Asycdialog.dismiss();
}
}
这是代码。
答案 0 :(得分:1)
尝试一下,让我知道它是否有效。
Asycdialog.show();
Query query = databaseReference.child("").orderByChild("cityWithCountry").equalTo(strings[0]);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
arrData.clear();
if (dataSnapshot.exists()) {
// dataSnapshot is the "issue" node with all children with id 0
for (DataSnapshot items : dataSnapshot.getChildren()) {
// do something with the individual "issues"
Log.d("QueryValues",""+items.getValue());
DataConstructor ds = items.getValue(DataConstructor.class);
arrData.add(ds);
**// check condition and hide your dialog here**
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});