Firebase使用endAt时间戳字段 [英] Firebase use endAt if timestamp field
问题描述
年龄:25
时间戳:1484089199472
code>
插入的记录:
@Exclude
public Map< String,Object> toMap(){
HashMap< String,Object> result = new HashMap<>();
result.put(age,age);
result.put(timestamp,ServerValue.TIMESTAMP);
返回结果;
如果我使用 endAt(24,age )
比它的工作好,这个项目是不可见的。
但是,如果我想在时间戳endAt使用它不工作。例如:
endAt(100000,timestamp)
,该行保持可见状态。
为什么?
这是检查类型吗?
如何过滤?
我没有任何意见! :(
我希望有人可以帮助我!
谢谢!
编辑: b
查询获取实际的日行
@Override
public Query getQuery DatabaseReference databaseReference){
Date endTime = new Date();
endTime.setHours(23);
endTime.setMinutes(59);
endTime.setSeconds(59);
return databaseReference
.child(toplist)
.endAt(endTime.getTime(),timestamp)
.orderByChild(point)
.limitToFirst(50);
}
数据
@IgnoreExtraProperties
public class Rank {
public int number;
public String username ;
public int point;
// [START post_to_map]
@Exclude
public Map< String,Object> toMap(){
HashMap< Stri ng,Object> result = new HashMap<>();
result.put(number,number);
result.put(username,username);
result.put(point,point);
result.put(timestamp,ServerValue.TIMESTAMP);
返回结果;
$ b $ / code $ / pre
添加新的随机项
private void writeNewRandomTopListItem(){
Random rnd = new Random();
Rank item = new Rank();
item.username =Test+ rnd.nextInt(20000);
item.point = rnd.nextInt(2000000);
mDatabase.child(toplist)。push()。setValue(item.toMap());
$ b $适配器
$ pre> 查询postsQuery = getQuery(mDatabase);
mAdapter = new FirebaseRecyclerAdapter< Rank,PostViewHolder>(Rank.class,R.layout.item_post,PostViewHolder.class,postsQuery){
@Override
protected void populateViewHolder(final PostViewHolder viewHolder,final Rank模型,final int位置){
viewHolder.bindToPost(model,position);
}
};
mRecycler.setAdapter(mAdapter);
...
toplist
-Ka8m3hN-vRjPZhKH3Rt
number:0
point:1269424
timestamp:148407253222471
username:Test 19727
-Ka8m3hWRIQKL7faNqUS
number:0
point:1398061
timestamp:1484075322472
username:Test 17679
解决方案您应该通过时间戳
项目由客户端的 points
。这是描述这里。
我建议您使用这个查询来代替:
@Override
public Query getQuery(final DatabaseReference databaseReference){
Date endTime = new Date();
endTime.setHours(23);
endTime.setMinutes(59);
endTime.setSeconds(59);
return databaseReference.child(toplist)。orderByChild(timestamp)
.endAt(endTime.getTime())。limitToFirst(50);
$ b这样,您就可以告诉Firebase通过他们的某个子节点对您的节点进行排序在这种情况下,它将是 timestamp
。希望这有助于。
接下来,您需要在客户端对数据进行排序。一个问题是,从firebase的实时更改不会在 RecyclerView
内重新排序您的项目。例如。
我再次建议阅读这个回答。
I have records with this fields:
age: 25
timestamp: 1484089199472
This records inserted by:
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("age", age);
result.put("timestamp", ServerValue.TIMESTAMP);
return result;
}
If i use endAt(24,"age")
than its work good, the item is invisible.
But if i want to use endAt in timestamp its not work. For example:
endAt(100000,"timestamp")
and the row is stay visible.
Why?
This is check the type?
How can i filter it?
I dont have any idea! :(
I hope someone can help me!
Thanks!
EDIT:
Query for get the actually day rows
@Override
public Query getQuery(final DatabaseReference databaseReference) {
Date endTime = new Date();
endTime.setHours(23);
endTime.setMinutes(59);
endTime.setSeconds(59);
return databaseReference
.child("toplist")
.endAt(endTime.getTime(),"timestamp")
.orderByChild("point")
.limitToFirst(50);
}
Data
@IgnoreExtraProperties
public class Rank{
public int number;
public String username;
public int point;
// [START post_to_map]
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("number", number);
result.put("username", username);
result.put("point", point);
result.put("timestamp", ServerValue.TIMESTAMP);
return result;
}
}
Add new random item
private void writeNewRandomTopListItem() {
Random rnd = new Random();
Rank item = new Rank();
item.username = "Test " + rnd.nextInt(20000);
item.point = rnd.nextInt(2000000);
mDatabase.child("toplist").push().setValue(item.toMap());
}
Adapter
Query postsQuery = getQuery(mDatabase);
mAdapter = new FirebaseRecyclerAdapter<Rank, PostViewHolder>(Rank.class, R.layout.item_post, PostViewHolder.class, postsQuery) {
@Override
protected void populateViewHolder(final PostViewHolder viewHolder, final Rank model, final int position) {
viewHolder.bindToPost(model,position);
}
};
mRecycler.setAdapter(mAdapter);
...
toplist
-Ka8m3hN-vRjPZhKH3Rt
number: 0
point: 1269424
timestamp: 148407253222471
username: "Test 19727"
-Ka8m3hWRIQKL7faNqUS
number: 0
point: 1398061
timestamp: 1484075322472
username: "Test 17679"
解决方案 You should order your nodes by the timestamp
child first and secondly order the received items by points
on the client side. This was described here.
I would suggest you to use this query instead:
@Override
public Query getQuery(final DatabaseReference databaseReference) {
Date endTime = new Date();
endTime.setHours(23);
endTime.setMinutes(59);
endTime.setSeconds(59);
return databaseReference.child("toplist").orderByChild("timestamp")
.endAt(endTime.getTime()).limitToFirst(50);
}
This way you are telling Firebase to sort your nodes by one of their child nodes which in this case would be timestamp
. Hope this helps.
Next you would need to sort your data on the client side. A problem of this would be that realtime changes from firebase would not reorder your items inside a RecyclerView
for example.
Once again I recommend reading through this answer.
这篇关于Firebase使用endAt时间戳字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!