2017-01-01 104 views
3

我有一个FireBase后端,它具有如此的时间戳...有许多这些节点是已推送的对象。基于ServerValue.TIMESTAMP查询Firebase

DB set up

每个节点都有一个时间标记与ServerValue.TIMESTAMP设置。

在数据模型类设置的服务器时间

private HashMap<String, Object> timestampCreated; 

//set time stamp in constructor when new obj is created 
public DataModel(...){ 
// TIME STAMP 
    HashMap<String, Object> timestampCreatedObj = new HashMap<String, Object>(); 
    timestampCreatedObj.put("date", ServerValue.TIMESTAMP); 
    this.timestampCreated = timestampCreatedObj; 
} 

//getting timestamp 

    public HashMap<String, Object> getTimestampCreated(){ 
    return timestampCreated; 
} 

@Exclude 
public long getTimestampCreatedLong(){ 
    return (long)timestampCreated.get("date"); 
} 

以上所有

工作正常插入数据库,我可以检索的时间。

在另一个类中,我想在FirebaseListAdapter上设置查询 设置查询可以让我们显示最近5天内的项目。

long daysInpast= new Date().getTime() - TimeUnit.MILLISECONDS.convert(5, TimeUnit.DAYS); 

然后,我设置了查询

 Query queryDays= mDatabaseQuery.child("timestampCreated").orderByChild("date").endAt(daysInpast); 

final FirebaseListAdapter<myData> adapterQuery = new FirebaseListAdapter<myData> 
       (getActivity(), myData.class, R.layout.list_item, queryDays) {//do stuff} 

它我查询我不知道,我的清单将返回空,如果我只是用我的FirebaseListAdapter,而没有经过DB查询我的名单查询按预期填充。我怀疑它的数据是如何布置的?事实上,我的时间点击是在一个子节点是一个问题?

我刚刚使用

Query queryDays= mDatabaseQuery.orderByChild("date").endAt(daysInpast); 

还,但没有韦尔

任何帮助赞赏

回答

2

你需要运行在上面的孩子的位置,你想返回的查询,试图其中看起来是数据库的根源(请不要发布带有模糊文本的图片,下次将实际的JSON发布为文本)。

如果你的确需要你的数据库的根目录下的水平,你可以查询与日期:

DatabaseReference root = FirebaseDatabase.getInstance().getReference(); 
Query query = root.orderByChild("timestampCreated/date").endAt(daysInPast); 
+0

谢谢,这是我需要的,现在的工作,有关图像的道歉,将发布JSON在未来。 – RoRo88