我的代码将逐行处理,而不等待从数据库加载的值, 我的意思是如果它在步骤1中是当前的并且尝试从数据库获取数据它将移动到另一个步骤2而仍然数据不是完全获取 任何人都可以帮助我尽快解决这个问题
private List<ContactInfo> Go(final List<ContactInfo> result) {
myRef1 = FirebaseDatabase.getInstance().getReference().child("0532").child("TL");
myRef1.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final long[] pendingLoadCount = { dataSnapshot.getChildrenCount() };
for (DataSnapshot itemSnapshot: dataSnapshot.getChildren()){
Log.v("ABCD", itemSnapshot.getValue().toString());
value = itemSnapshot.getValue().toString();
// String parts[] = value.split("#");
final ContactInfo ci = new ContactInfo();
ci.name = value;
Query myRef2 = FirebaseDatabase.getInstance().getReference().child("0532").child(DD).child(value);
myRef2.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String ,Object> key = (Map<String, Object>) dataSnapshot.getValue();
if(dataSnapshot.hasChild("settime"))
{
// Log.v("ABCD", dataSnapshot.getValue().toString());
// Log.v("ABCD", key.values().toString());
ci_surname = (String) key.get("settime");
ci_email = (String) key.get("settime");
ci_title = (String) key.get("settime");
Log.v("ABCD", "Nothing found 2"+ci_surname);
ci.surname = ci_surname ;
ci.email = ci_email ;
ci.title = ci_title;
result.add(ci);
} else{
Log.v("ABCD", "Nothing found 2"+ci_surname);
ci.surname = "ABCD" ;
ci.email = "XYZ" ;
ci.title = "FFF";
result.add(ci);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
pendingLoadCount[0] = pendingLoadCount[0] - 1;
if (pendingLoadCount[0] == 0) {
Log.v("ABCD", "Nothing found");
recList.setAdapter(ca);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return result;
}
答案 0 :(得分:0)
Firebase 在AsyncTask中运行,因此您不必使用另一个 AsynTask
等待的最佳方法是使用 ProgressDialog
让我们说你有这个函数返回progressdialog
private ProgressDialog MyProgressDialog() {
final ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("loading ...");
progressDialog.setCancelable(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
return progressDialog;
}
然后使用它
final ProgressDialog myownpd = MyProgressDialog();
并在您的Load_data_from_firebase函数中
private List<ContactInfo> Go(final List<ContactInfo> result) {
myownpd.show();
myRef1 = FirebaseDatabase.getInstance().getReference().child("0532").child("TL");
myRef1.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final long[] pendingLoadCount = { dataSnapshot.getChildrenCount() };
for (DataSnapshot itemSnapshot: dataSnapshot.getChildren()){
Log.v("ABCD", itemSnapshot.getValue().toString());
value = itemSnapshot.getValue().toString();
// String parts[] = value.split("#");
final ContactInfo ci = new ContactInfo();
ci.name = value;
Query myRef2 = FirebaseDatabase.getInstance().getReference().child("0532").child(DD).child(value);
myRef2.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String ,Object> key = (Map<String, Object>) dataSnapshot.getValue();
if(dataSnapshot.hasChild("settime"))
{
// Log.v("ABCD", dataSnapshot.getValue().toString());
// Log.v("ABCD", key.values().toString());
ci_surname = (String) key.get("settime");
ci_email = (String) key.get("settime");
ci_title = (String) key.get("settime");
Log.v("ABCD", "Nothing found 2"+ci_surname);
ci.surname = ci_surname ;
ci.email = ci_email ;
ci.title = ci_title;
result.add(ci);
} else{
Log.v("ABCD", "Nothing found 2"+ci_surname);
ci.surname = "ABCD" ;
ci.email = "XYZ" ;
ci.title = "FFF";
result.add(ci);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
pendingLoadCount[0] = pendingLoadCount[0] - 1;
if (pendingLoadCount[0] == 0) {
Log.v("ABCD", "Nothing found");
recList.setAdapter(ca);
}
myownpd.cancel();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
myownpd.cancel();
}
});
return result;
}