- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在尝试对来自 firebase 数据库的数据进行分页数周时遇到了一个很好的问题,现在都无济于事。我尝试了从 android 文档到 SO 的所有示例,但仍然无法正常工作。下面是我的数据节点的截图
下面是我的代码,我使用 recycleview 进行连续滚动。
commentDatabaseReference = FirebaseDatabase.getInstance().getReference().child(CHANNEL_COMMENT).child(postId);
commentDatabaseReference.orderByKey().limitToLast(TOTAL_ITEM_EACH_LOAD).addListenerForSingleValueEvent(commentValueEventListener);
ValueEventListener commentValueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.hasChildren()) {
currentPage--;
}
int counter = 0;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
commentId = snapshot.getKey();
Comment comment = snapshot.getValue(Comment.class);
commentList.add(comment);
adapter.notifyDataSetChanged();
}
//adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
Recycleview 连续滚动监听器
recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
Log.e("more", "onload");
loadMoreData();
}
});
private void loadData() {
Log.e("loading", ""+ commentId);
commentDatabaseReference.orderByChild("timestamp").startAt(commentId).limitToLast(TOTAL_ITEM_EACH_LOAD)
.addListenerForSingleValueEvent(commentValueEventListener);
}
private void loadMoreData(){
//if ((currentPage * TOTAL_ITEM_EACH_LOAD) <= commentCount) {
currentPage++;
loadData();
//}
}
结论:例如,如果我有 20 个偏移量为 10 的数据,并且我将 orderby 与 limitToLast 一起使用,我会从顶部开始获取最后 10 个数据,即我从 11 - 20 而不是 20 - 11 获取数据。另外注意到当我结合 startAt
时,有时我会得到无穷无尽的数据最佳答案
最终在 faruk 的提示下解决了这个问题:
下面的代码方法将数据从 firebase 加载到 recycleview
private void loadData() {
Query query;
if (commentId == null) {
query = commentDatabaseReference.orderByChild("timestamp").limitToLast(TOTAL_ITEM_EACH_LOAD);
} else {
query = commentDatabaseReference.orderByChild("timestamp").startAt(inverseTimestamp).limitToFirst(TOTAL_ITEM_EACH_LOAD);
}
query.addListenerForSingleValueEvent(commentValueEventListener);
}
private void loadMoreData(){
//commentCount: is the total comments stored in firebase database
if ((currentPage * TOTAL_ITEM_EACH_LOAD) <= commentCount) {
currentPage++;
loadData();
}
}
在 recycleview 滚动监听器上,我调用了 loadMoreData 方法以从 firebase 获取更多数据
recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view) {
//Load more data
loadMoreData();
}
});
Firebase 数据库监听器,用于监听获取的数据
ValueEventListener commentValueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.hasChildren()) {
currentPage--;
}
int counter = 0;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
commentId = snapshot.getKey();//Index key for the node
Comment comment = snapshot.getValue(Comment.class);
++counter;
//Inverse timestamp
inverseTimestamp = comment.getTimestamp();
//store nth - 1 data into the arraylist to avoid duplicates
if (counter < TOTAL_ITEM_EACH_LOAD) {
commentList.add(comment);
adapter.notifyDataSetChanged();
}
}
//adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
关于android - Firebase 数据库升序分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45644282/
我正在做作业,经过几天的努力,我无法弄清楚为什么在实现归并排序后,我的列表仅包含链接列表中的最后一个对象。它不输出我的整个链表,只输出最后一个对象。如何更改代码以阻止列表在一个对象之后变为 null。
我想对一列进行排序(它是一个带有 Y/N 的标志列)。它应该在每次点击时在升序/降序之间切换。 我的代码不起作用..我是 VBA 新手。请提供任何帮助。 Private Sub CommandButt
我对如何让它正常工作有点困惑。我需要从用户那里获取数字(直到他们输入负数或达到最大大小),并且对于他们添加的每个数字,将其按升序插入到正确的索引中。现在,由于某种原因,即使我定义了常量 10,我的数组
我相当困惑如何创建一个按钮,将打印到 php 文件的表中的数据按升序或降序排序。 "> Order by Week Sort Week 这是我想要实现的一个简单示例,我只是停留在 php
我在使用 C++ 中的 priority_queue 时遇到问题,我有一个优先级队列 vector ,优先级队列包含多个 Person 对象。现在,我希望 priority_queue 根据年龄对 P
我正在使用 Lodash 按列对表中的数据进行排序。当我单击表格列标题中的箭头时,该特定表格列将按升序或降序排序。但是,我希望每一列首先按升序排序,而不管其他列的当前顺序如何。现在,我的函数只根据当前
如果事先知道哪些列可用,则以下代码可以重新排列列,但如果想按降序/升序重新排列列怎么办? StackOverflow 上有一些类似的帖子,但没有一篇可以在事先不知道哪些列可用的情况下这样做。 ty
在 woocommerce 中,我使用以下代码添加了自定义费用: add_action( 'woocommerce_cart_calculate_fees', 'custom_fee_based_on
这可以很好地以最多 1000 个项目的步长对数据进行分页: var q1 = (from book in table.CreateQuery() where book.PartitionKe
您好,我正在使用以下内容对表适配器返回的数据表的结果进行排序 Dim spots = myDataTable.Where(Function(t) t.UserID = 1).OrderByDesce
这可以很好地以最多 1000 个项目的步长对数据进行分页: var q1 = (from book in table.CreateQuery() where book.PartitionKe
我正在尝试获取数据库中最近的 n 个条目的列表,但将它们按升序排序。 显然我可以使用以下方法获取前 n 个条目: SELECT owner_id,message FROM messages WHERE
我尝试使用此方法将数据提取到 mysql 表 $query=$conn->query("SELECT * FROM users ORDER BY id_user ASC"); 这是我的表结构 用户 i
我正在使用 NSFetchedResultsController 在列表中显示对象 Event。 Event 对象具有 startDate 属性和 eventType 属性,它是 CheckIn 类型
我有以下代码/数据: import numpy as np data = np.array([ [12, 1, 0.7, 0], [13, 2, 0.5, 1], [41, 3
所以我是 C++ 的新手,我正在尝试一些初学者练习,这是问题所在:我必须按升序和降序对整数数组进行排序,但每次我尝试按升序排序时,都会出现 0在我的数组中无处替换以前的数组整数。只有当我使用“升序”选
在我的应用程序中,我有一个任务列表(不,它不仅仅是另一个待办事项应用程序),我使用 NSFetchedResultsController 在 UITableView 中显示任务。这是相关的初始化代码:
本人由于项目开发中需要对查询结果list进行排序,这里根据的是每一个对象中的创建时间降序排序。本人讲解不深,只实现目的,如需理解原理还需查阅更深的资料。 1.实现的效果 2.创建排序的对象
ORDER BY _column1, _column2; /* _column1升序,_column2升序 */
我需要插入两个值 num1 = 50和 num2 = 80成一个已按升序排序的数组。我不能使用动态数组或列表。也没有结构或类。这是一个类作业,所以我必须遵循指导方针。教授建议我新建一个数组,newar
我是一名优秀的程序员,十分优秀!