【发布时间】:2020-08-04 15:22:24
【问题描述】:
我正在创建一个应用程序,该应用程序的主要功能是用户将能够计划未来的旅行,并且该应用程序会通知他何时开始在那里导航。我想根据日期和时间(当前日期最接近的行程排在第一位)存储和排序即将到来的行程。
我在 StackOverflow 中找到了一些关于日期问题的答案,但所有答案都是关于使用 ServerValue.TIMESTAMP 从系统获取当前时间(并使用时间戳将其存储在 Firebase 实时数据库中)。
这仅适用于存储当前日期和时间。我有兴趣获取未来日期的时间戳。
我目前将行程的数据作为行程对象写入数据库中,如下所示:
//Store date,time and destination on a new Trip.
datePassed = dateText.getText().toString();
timePassed = timeText.getText().toString();
trip = new Trip(destinationPassed, datePassed, timePassed);
//Get uId of the Firebase User.
String uId = currentFirebaseUser.getUid();
//Create a unique Hash Key for the Trip. (Used to Distinguish trips)
String tripId = Integer.toString(trip.getTripId(destinationPassed, datePassed, timePassed));
//Store the trip on Firebase RealTime Database.
DatabaseReference childReff = dbRef.child(uId).child("trips").child(tripId);
childReff.setValue(trip);
然后我像这样检索数据:
//Get uId of the user
final String uId = currentFirebaseUser.getUid();
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.child(uId).child("trips").getChildren()) {
//Here I access each child separately (destination, date, time)
date = child.child("date").getValue().toString();
destination = child.child("destination").getValue().toString();
time = child.child("time").getValue().toString();
我的问题是,对于未来设置的行程,我如何才能实现按升序存储和行程(当前日期最接近的行程是第一个)?
【问题讨论】:
标签: java android firebase firebase-realtime-database