我的项目中有一个日历页面。当我运行代码时,addListenerForSingleValueEvent
函数会获取随机数据。
当我在“调试”模式下运行时,数据以顺序方式出现。
为什么正常模式与调试模式不同?
该Firebase数据库;
"0588326969" : {
"196" : {
"RUKIYE POLAT" : {
"-LQJgZfDnhYG9bva1fV8" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "02/11/2018,16:52:24"
},
"-LQJg_D6BpNDWb1QpMrH" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "02/11/2018,16:52:25"
},
"-LQKOe-iAZntPmd5rTng" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "02/11/2018,20:09:22"
},
"-LQN65ZTecjRHRqr3Mt8" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "03/11/2018,8:47:10"
},
"-LQN66FbJ9B60gh0QasO" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "03/11/2018,8:47:11"
},
"-LQOl_D-YPkBorwd1ObT" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "03/11/2018,16:32:24"
},
"-LQSF9pnqcg9k6QFWJ9l" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "04/11/2018,8:44:53"
},
"-LQTrfXvBn_BXeKn7RBv" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "04/11/2018,16:17:09"
},
"-LQZ7kqtFLen3LxnVnAv" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "05/11/2018,16:49:51"
},
"-LQZgsTCEzpvyvzmBIzU" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "05/11/2018,19:27:41"
},
"-LQiRgVjqAnUamgPcNgj" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "07/11/2018,16:53:10"
},
"-LQiRh33jcpE2GLifTvz" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "07/11/2018,16:53:11"
},
"-LQj8tjeKtx0AJHt88G6" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "07/11/2018,20:10:40"
},
"-LQnaBSEUOxeBCUsO48y" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "08/11/2018,16:52:46"
},
"-LQnaBziiUf2NAURu2uF" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "08/11/2018,16:52:47"
},
"-LQoIpof4apMbNW2pzmS" : {
"adSoyad" : "RUKIYE POLAT",
"tarih" : "08/11/2018,20:12:11"
}
}
},
}
我的代码:
Date date1=null;
String bugununtarihi=new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()).format(new Date());
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
try {
date1 = sdf.parse(bugununtarihi);
}
catch (ParseException e){
e.printStackTrace();
}
Calendar cal1=null;
Calendar cal2=null;
cal1=Calendar.getInstance();
cal1.setTime(date1);
cal2=Calendar.getInstance();
cal2.setTime(date1);
cal1.add(Calendar.DATE,-5);
final ArrayList<HashMap<String,String>> veriler=new ArrayList<>();
while (!cal1.after(cal2)){
String birincitarih = sdf.format(cal1.getTime());
cal1.add(Calendar.DATE, 1);
DatabaseReference dbGelenler=db.getReference("0588326969"+"/"+"196"+"/"+"RUKIYE POLAT");
Query query=dbGelenler.orderByChild("tarih").startAt(birincitarih).endAt(birincitarih+"\uf8ff");
query.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
List<String> isimler=new ArrayList<>();
for (DataSnapshot anahtar:dataSnapshot.getChildren()){
String isim=anahtar.getValue(ornekogrencigetir.class).getAdSoyad();
String tarih=anahtar.getValue(ornekogrencigetir.class).getTarih();
HashMap<String,String> user=new HashMap<>();
String[] seperated1;
seperated1 = tarih.split(",");
user.put("hashtarih", seperated1[0]);
user.put("hashsaat", seperated1[1]);
isimler.add(isim); veriler.add(user);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, veriler, R.layout.takvimsatir,
new String[]{"hashtarih", "hashsaat"}, new int[]{R.id.takvimsatirtarih, R.id.takvimsatirsaat}) {
};
mainlist.setAdapter(adapter);
}
}
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
在调试模式下,日期是按顺序排列的,例如02,03,04,05,...
When I run debug when I run application mode output is: output is: 04/11/2018 08:44:53 07/11/2018 16:53:10 04/11/2018 16:17:09 07/11/2018 16:53:11 05/11/2018 16:49:51 07/11/2018 20:10:40 05/11/2018 19:27:41 05/11/2018 16:49:51 07/11/2018 16:53:10 05/11/2018 19:27:41 07/11/2018 16:53:11 04/11/2018 08:44:53 07/11/2018 20:10:40 04/11/2018 16:17:09 08/11/2018 16:52:46 08/11/2018 16:52:46 08/11/2018 16:52:47 08/11/2018 16:52:47 08/11/2018 20:12:11 08/11/2018 20:12:11
注意:我在不同的项目中使用相同的代码。项目中的日期按顺序排列,就像调试模式的值一样。 我想按顺序获取日期,就像上面的调试模式一样
回答:我使用了setPersistenceEnabled(true); 我创建了一个类 DataBaseUtil:
public class DataBaseUtil {
private static FirebaseDatabase mDatabase;
public static FirebaseDatabase getDatabase() {
if (mDatabase == null) {
mDatabase = FirebaseDatabase.getInstance();
mDatabase.setPersistenceEnabled(true);
}
return mDatabase;
}}
我使用了此类 db = DataBaseUtil.getDatabase(),而不是db = FirebaseDatabase.getInstance() 并且我为每个FirebaseDatabase数据库添加了private,因为 私有FirebaseDatabase数据库;