【发布时间】:2020-10-15 16:58:14
【问题描述】:
我有以下表格
- 餐厅
- 表
- 预订
我正在为用户输入日期和时间的餐厅创建餐桌可用性搜索。结果显示可用 AKA 尚未在所选日期/时间保留的表。
- 我正在检查特定餐厅的餐桌日期/时间:
- 此 objectID 是从上一个活动(餐厅配置文件)传递的
ParseQuery<ParseObject> restaurant = ParseQuery.getQuery("Restaurants");
restaurant.get(objectID);
- 我从 Table 表中获取与 Restaurant 关联的表
ParseQuery<ParseObject> tableQuery = ParseQuery.getQuery("Table");
tableQuery.whereMatchesQuery("restaurant_id", restaurant);
- 然后我在 Reservation 表中获取与这些表关联的预订,并将它们收集到 reservationResults 列表中
ParseQuery<ParseObject> reservations = ParseQuery.getQuery("Reservation");
reservations.whereMatchesQuery("table_id", tableQuery);
reservations.include("table_id");
List<ParseObject> reservationResults = reservations.find();
- 从这里我尝试遍历 ParseObjects 列表并对预订日期/时间进行一些计算/检查。 IF 开始时选择的日期/时间与任何已预订的日期/时间发生冲突 --> THEN 我抓住那些发生冲突的表 objectId 并添加它们到“列表 tableListToExclude”。然后使用此列表检查某些表是否未显示在 recyclerView 中,如果它们是为选定时间保留的
for(int i = 0; i < reservationResults.size(); i++) {
Date reservationDateTime = reservationResults.get(i).getDate("time");
ParseObject reservation = reservationResults.get(i);
ParseObject table = reservation.getParseObject("table_id");
String tableId = table.getObjectId();
Log.i(TAG, "|||||||||||TEST|||||||| ------> " + tableId);
// calculates the two hour window of the reservation
Calendar calRes = Calendar.getInstance();
assert reservationDateTime != null;
calRes.setTime(reservationDateTime);
calRes.add(Calendar.HOUR_OF_DAY, 2);
//sets the end time of two hour window
Date reservationEndTime = calRes.getTime();
// calculates two hours window of selected time
Calendar calSelected = Calendar.getInstance();
calSelected.setTime(combinedSelectedDate);
calSelected.add(Calendar.HOUR_OF_DAY, 2);
// sets the end time of the selected
Date selectedEndTime = calSelected.getTime();
//compares the selected date/time and it's 2 hour window against the Reservation table
if((combinedSelectedDate.after(reservationDateTime) && combinedSelectedDate.before(reservationEndTime)) || (selectedEndTime.after(reservationDateTime) && selectedEndTime.before(reservationEndTime))) {
Log.i(TAG, "||||||||||||TEST||||||||||||| ---------> Some dates fall in the 2 hour window");
tableListToExclude.add(tableId);
System.out.println(Arrays.asList(tableListToExclude));
}
}
我在 logcat 中得到的错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.parse.ParseObject.getObjectId()' on a null object reference
我被困在这里,但我相信这只是因为没有完全理解 Parse 以及如何查询关系或指针等,...我需要在这里创建另一个查询吗?或者以某种方式获取 ParseRelation,然后将其放入 ParsObject,然后获取 objectId?我在这里尝试了几种不同的场景,但没有运气,我想我已经很接近了,但只是有些东西我没有完全理解或遗漏......
此外,也欢迎任何有关根据日期/时间过滤掉我的 recyclerview 项目的更好方法的建议!如果您需要更多代码或说明,请告诉我。谢谢!
编辑
当前版本我在开始时对查询进行了调整,并且还使用了 findInBackground() 而不是 find():
// ParseQuery<ParseObject> restaurant = ParseQuery.getQuery("Restaurants");
// restaurant.get(objectID);
ParseQuery<ParseObject> tableQuery = ParseQuery.getQuery("Table");
tableQuery.whereEqualTo("restaurant_id", ParseObject.createWithoutData("Restaurants", objectID));
ParseQuery<ParseObject> reservations = ParseQuery.getQuery("Reservation");
reservations.whereMatchesQuery("table_id", tableQuery);
reservations.include("table_id");
reservations.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> reservationResults, ParseException e) {
for(int i = 0; i < reservationResults.size(); i++) {
Date reservationDateTime = reservationResults.get(i).getDate("time");
ParseObject reservation = reservationResults.get(i);
ParseObject table = reservation.getParseObject("table_id");
String tableId = table.getObjectId();
System.out.println(tableId);
// calculates the two hour window of the reservation
Calendar calRes = Calendar.getInstance();
assert reservationDateTime != null;
calRes.setTime(reservationDateTime);
calRes.add(Calendar.HOUR_OF_DAY, 2);
//sets the end time of two hour window
Date reservationEndTime = calRes.getTime();
// calculates two hours window of selected time
Calendar calSelected = Calendar.getInstance();
calSelected.setTime(combinedSelectedDate);
calSelected.add(Calendar.HOUR_OF_DAY, 2);
// sets the end time of the selected
Date selectedEndTime = calSelected.getTime();
//compares the selected date and time against the reservation times of the tables
if((combinedSelectedDate.after(reservationDateTime) && combinedSelectedDate.before(reservationEndTime)) || (selectedEndTime.after(reservationDateTime) && selectedEndTime.before(reservationEndTime))) {
Log.i(TAG, "||||||||||||TEST||||||||||||| ---------> Some dates fall in the 2 hour window");
tableListToExclude.add(tableId);
System.out.println(Arrays.asList(tableListToExclude));
}
}
}
});
【问题讨论】:
标签: java android parse-platform back4app