首先,我对Android非常陌生,可能只是坚持我的逻辑......我现在试图解决这个问题好几天了。我正在制作一个应用程序,您可以在其中跟踪您吸烟的香烟。我的树看起来像这样:
当我添加条目时,存储以毫秒为单位的时间以及与最后一个子项的差异。现在当我添加一个条目(在现有条目之前)(通过Date / TimePicker)时,数据按" time_in_millis"正确排序,但" diff_in_ms"是错的:
After adding an entry in between
这是我从前一个条目获取数据以计算diff_in_ms的代码:
final Query lastQuery = drLast.child(db_date);
lastQuery.orderByChild("time_in_millis").limitToLast(1);
lastQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
for (DataSnapshot LastDataSnap: dataSnapshot.getChildren()) {
LastEntry getLast = LastDataSnap.getValue(LastEntry.class);
ltim = getLast.getTime_in_millis();
}
} else {
ltim = calendar.getTimeInMillis();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});

这是处理数据以填充ListView的类:
public class DataHandler {
//Handles Data before populating the List
private long time_in_millis, ms, ms1, diff_in_ms;
private String date, time, diff, db_date;
public DataHandler() {
}
public DataHandler(long time_in_millis, String date, String time, String diff, long diff_in_ms, String db_date) {
this.time_in_millis = time_in_millis;
this.date = date;
this.time = time;
this.diff = diff;
this.diff_in_ms = diff_in_ms;
this.db_date = db_date;
}
public long getTime_in_millis() {
return time_in_millis;
}
public String getDate() {
ms = time_in_millis;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.");
date = String.valueOf(dateFormat.format(ms));
return date;
}
public String getTime() {
ms1 = time_in_millis;
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
time = String.valueOf(timeFormat.format(ms1));
return time;
}
public String getDiff() {
// I tried to get here the time_in_millis from the last child and calculate it, but the app always got stuck on the device
if (diff_in_ms != 0) {
diff = String.format("%02d" + "h " + "%02d" + "m", TimeUnit.MILLISECONDS.toHours(diff_in_ms),
TimeUnit.MILLISECONDS.toMinutes(diff_in_ms) % TimeUnit.HOURS.toMinutes(1));
} else {
diff = "---";
}
return diff;
}
public long getDiff_in_ms() {
return diff_in_ms;
}
public String getDb_date() {
return db_date;

我尝试在我的模型类中填充listview时计算差异,但应用程序总是挂在手机上。 现在我的问题是:如果diff_in_ms是< 0然后我必须更新" diff_in_ms"以下所有孩子的节点,因为它与之前的孩子有关,而不一定与最后一个孩子有关。怎么做?
或者从DB获取数据时计算它是否更好?
感谢任何帮助...
编辑:现在它在我的模型类中使用Query运行:
public class DataHandler {
//Handles Data before populating the List
private long time_in_millis, ms, ms1, ltim, diff_in_ms;
private String date, time, diff, db_date;
DatabaseReference drLast = FirebaseDatabase.getInstance().getReference();
Calendar calendar = Calendar.getInstance();
public DataHandler() {
}
public DataHandler(long time_in_millis, String date, String time, String diff, String db_date) {
this.time_in_millis = time_in_millis;
this.date = date;
this.time = time;
this.diff = diff;
this.db_date = db_date;
}
public long getTime_in_millis() {
return time_in_millis;
}
public String getDate() {
ms = time_in_millis;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.");
date = String.valueOf(dateFormat.format(ms));
return date;
}
public String getTime() {
ms1 = time_in_millis;
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
time = String.valueOf(timeFormat.format(ms1));
return time;
}
public String getDiff() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
db_date = format.format(calendar.getTime());
final Query lastQuery = drLast.child(db_date);
lastQuery.orderByChild("time_in_millis").limitToLast(1);
lastQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
for (DataSnapshot LastDataSnap: dataSnapshot.getChildren()) {
DataToSave getLast = LastDataSnap.getValue(DataToSave.class);
ltim = getLast.getTime_in_millis();
}
} else {
ltim = calendar.getTimeInMillis();
}
//Calculate the difference
diff_in_ms = time_in_millis - ltim;
if (diff_in_ms != 0) {
diff = String.format("%02d" + "h " + "%02d" + "m", TimeUnit.MILLISECONDS.toHours(diff_in_ms),
TimeUnit.MILLISECONDS.toMinutes(diff_in_ms) % TimeUnit.HOURS.toMinutes(1));
} else {
diff = "---";
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
return diff;
}
public String getDb_date() {
return db_date;
}
}

但它在ListView中没有显示差异值。