2 回答
TA贡献1784条经验 获得超2个赞
据我所知,你最初这样做:
Query query = mRootDatabase.limitToFirst(20);
FilterData filterData = new FilterData(query,this);
并FilterData接着做:
public FilterData(Query mQueryDatabase, Context context) {
this.mQueryDatabase = mQueryDatabase;
this.userPojo = new UserPojo();
this.mContext = context;
}
接着:
mQueryDatabase.orderByKey().startAt(oldestUserID).limitToFirst(20).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
fetchData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
在这一点上,如果我们将这些东西链接在一起,你有:
mRootDatabase
.limitToFirst(20)
.orderByKey()
.startAt(oldestUserID)
.limitToFirst(20)
.addListenerForSingleValueEvent(
...
由于您limitToFirst(20)在那里两次,它解释了为什么您会收到错误消息。
我的直觉是,您想将查询的根引用传递给FilterData,因此:
FilterData filterData = new FilterData(mRootDatabase,this);
或者,您可以在 之外构建基本查询(查询的所有部分都是常量)FilterData:
Query query = mRootDatabase.orderByKey().limitToFirst(20);
然后在FilterData你只添加变量部分(startAt()子句):
mQueryDatabase.startAt(oldestUserID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
fetchData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors
}
});
TA贡献1831条经验 获得超4个赞
在这里,我完成了 Firebase-Chat-Pagination。检查这个。它可能对你有帮助。
private void loadDataFirst() {
mUserRefValueEventListener = mConversationRef.child(mLocation)
.limitToLast(TOTAL_ITEM_EACH_LOAD)
.orderByKey()
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// It should be null when new meesage sent by own
mLastDataSnapshot = null;
if (dataSnapshot.exists()) {
mChatList.clear();
for (DataSnapshot readDataSnapshot : dataSnapshot.getChildren()) {
ChatResponse chatModel = readDataSnapshot.getValue(ChatResponse.class);
if (chatModel != null) {
mChatList.add(chatModel);
}
// We need First DataSnapshot for pagination
if (mLastDataSnapshot == null) {
mLastDataSnapshot = readDataSnapshot;
}
}
mView.onFetchMessagesSuccess(mChatList);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void loadMoreDataPagination() {
if (mLastDataSnapshot != null && (mTotalCount == 0 || mView.getChatMessageCount() < mTotalCount)) {
mUserRefValueEventListener = mConversationRef.child(mLocation)
.orderByKey()
.endAt(mLastDataSnapshot.getKey())
.limitToLast(TOTAL_ITEM_EACH_LOAD)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
mChatList.clear();
for (DataSnapshot readDataSnapshot : dataSnapshot.getChildren()) {
ChatResponse chatModel = readDataSnapshot.getValue(ChatResponse.class);
if (chatModel != null) {
mChatList.add(chatModel);
}
// We need Last DataSnapshot for pagination while load more
if (mChatList.size() == 1) {
mLastDataSnapshot = readDataSnapshot;
}
}
// One item duplicates from last list so need to Remove it
mChatList.remove(mChatList.size() - 1);
mView.onFetchLoadMoreSuccess(mChatList, mTotalCount);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
mView.hideLoading();
}
});
}
}
添加回答
举报