2016-11-29 223 views
0

我在使用自定義適配器的活動中顯示一些帖子(由用戶創建)。在自定義適配器的構造函數中,我使用以下代碼來獲取查詢結果。與空值比較日期

ParseQuery query = new ParseQuery("Post"); 
query.whereWithinMiles("location_point", my_point, 500); 
query.whereGreaterThan("expire_date", new Date()); 
return query; 

目標是 - 用戶可以設置一個到期日期與每個崗位,這樣的職位將不再是到期後可見。我試圖比較expire_date列與當前時間,以檢查帖子是否過期。

query.whereGreaterThan("expire_date", new Date());稍後添加。先前創建的所有行在expire_date列中的值爲NULL

所以我每次運行查詢時,所有的在EXPIRE_DATE列約會對象行正在根據病情檢查。在expire_date列中具有空值的行永遠不會返回

注意:expire_date列不是必填字段(用戶可以選擇在創建帖子時將其留空)。

是的,我可以設置所有空/空字段的默認日期與過期日期進行比較。但我很想知道是否有辦法讓我可以返回所有具有空值的行,或者將當前時間與具有whereGreaterThan條件的空對象進行比較?

+1

你試過一個OR查詢嗎? http://stackoverflow.com/questions/28892973/multiple-combined-or-queries-using-android-parse – nasch

+0

謝謝隊友。我使用OR查詢完成了這項工作。 :) @nasch –

回答

1
ParseQuery notExpiredPosts = new ParseQuery("Post"); 
query.whereGreaterThan("expire_date", new Date()); 

ParseQuery noExpiredDate = new ParseQuery("Post"); 
query.doesNotExist("expire_date"); 

ParseQuery expiredDateQuery = ParseQuery.or(notExpiredPosts, noExpiredDate); 

ParseQuery query = new ParseQuery("Post"); 
query.whereWithinMiles("location_point", my_point, 500); 
query.whereMatchesKeyInQuery("objectId", "objectId", expiredDateQuery); 
return query; 

[(NotExpiredPost || NoExpiredDate)&(500英里範圍內location_point)

這是你想要的,不是嗎?

希望這會有所幫助:)

+0

我已經提出了一些你的代碼的語法修改。非常感謝你的指導方針,它幫助了很多! :) –

+0

我很高興它幫助。快樂編碼:) –