gpt4 book ai didi

android - Firebase 数据库升序分页

转载 作者:搜寻专家 更新时间:2023-11-01 08:25:33 27 4
gpt4 key购买 nike

我在尝试对来自 firebase 数据库的数据进行分页数周时遇到了一个很好的问题,现在都无济于事。我尝试了从 android 文档到 SO 的所有示例,但仍然无法正常工作。下面是我的数据节点的截图

enter image description here

下面是我的代码,我使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com