Android 在处理之前等待加载数据
我的代码将逐行处理,而不等待从数据库加载的值, 我的意思是,如果它当前在第1步,并且试图从数据库中获取数据,那么它将移动到另一个第2步,而仍然没有完全获取数据 有人能帮我尽快解决这个问题吗Android 在处理之前等待加载数据,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我的代码将逐行处理,而不等待从数据库加载的值, 我的意思是,如果它当前在第1步,并且试图从数据库中获取数据,那么它将移动到另一个第2步,而仍然没有完全获取数据 有人能帮我尽快解决这个问题吗 private List<ContactInfo> Go(final List<ContactInfo> result) { myRef1 = FirebaseDatabase.getInstance().getReference().child("0532").child(
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;
}
private List Go(最终列表结果){
myRef1=FirebaseDatabase.getInstance().getReference().child(“0532”).child(“TL”);
myRef1.addListenerForSingleValueEvent(新的ValueEventListener()){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
最终长[]pendingLoadCount={dataSnapshot.getChildrenCount()};
对于(DataSnapshot itemSnapshot:DataSnapshot.getChildren()){
Log.v(“ABCD”,itemSnapshot.getValue().toString());
value=itemSnapshot.getValue().toString();
//字符串部分[]=value.split(“#”);
最终联系人信息ci=新联系人信息();
ci.name=值;
Query myRef2=FirebaseDatabase.getInstance().getReference().child(“0532”).child(DD).child(value);
myRef2.addListenerForSingleValueEvent(新的ValueEventListener()){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
Map key=(Map)dataSnapshot.getValue();
if(dataSnapshot.hasChild(“设置时间”))
{
//Log.v(“ABCD”,dataSnapshot.getValue().toString());
//Log.v(“ABCD”,key.values().toString());
ci_姓氏=(字符串)key.get(“设置时间”);
ci_email=(字符串)key.get(“设置时间”);
ci_title=(字符串)key.get(“设置时间”);
Log.v(“ABCD”,“未找到2”+ci_姓氏);
ci.姓氏=ci_姓氏;
ci.email=ci_email;
ci.title=ci_title;
结果:添加(ci);
}否则{
Log.v(“ABCD”,“未找到2”+ci_姓氏);
ci.name=“ABCD”;
ci.email=“XYZ”;
ci.title=“FFF”;
结果:添加(ci);
}
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
pendingLoadCount[0]=pendingLoadCount[0]-1;
如果(pendingLoadCount[0]==0){
Log.v(“ABCD”,“未找到”);
重合闸设置适配器(ca);
}
}
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
返回结果;
}
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();
在您的加载数据中,从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;
}
private List Go(最终列表结果){
myownpd.show();
myRef1=FirebaseDatabase.getInstance().getReference().child(“0532”).child(“TL”);
myRef1.addListenerForSingleValueEvent(新的ValueEventListener()){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
最终长[]pendingLoadCount={dataSnapshot.getChildrenCount()};
对于(DataSnapshot itemSnapshot:DataSnapshot.getChildren()){
Log.v(“ABCD”,itemSnapshot.getValue().toString());
value=itemSnapshot.getValue().toString();
//字符串部分[]=value.split(“#”);
最终联系人信息ci=新联系人信息();
ci.name=值;
Query myRef2=FirebaseDatabase.getInstance().getReference().child(“0532”).child(DD).child(value);
myRef2.addListenerForSingleValueEvent(新的ValueEventListener()){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
Map key=(Map)dataSnapshot.getValue();
if(dataSnapshot.hasChild(“设置时间”))
{
//Log.v(“ABCD”,dataSnapshot.getValue().toString());
//Log.v(“ABCD”,key.values().toString());
ci_姓氏=(字符串)key.get(“设置时间”);
ci_email=(字符串)key.get(“设置时间”);
ci_title=(字符串)key.get(“设置时间”);
Log.v(“ABCD”,“未找到2”+ci_姓氏);
ci.姓氏=ci_姓氏;
ci.email=ci_email;
ci.title=ci_title;
结果:添加(ci);
}否则{
Log.v(“ABCD”,“未找到2”+ci_姓氏);
ci.name=“ABCD”;
ci.email=“XYZ”;
ci.title=“FFF”;
结果:添加(ci);
}
}
@凌驾
已取消的公共void(DatabaseError DatabaseError){
}
});
pendingLoadCount[0]=pendingLoadCount[0]-1;
如果(pendingLoadCount[0]==0){
Log.v(“ABCD”,“未找到”);
重合闸设置适配器(ca);
}
myownpd.cancel();
}
}
@凌驾
已取消的公众假期(D)