尝试加载新数据时 Firebase LimitToLast 崩溃

在将 20 个用户发布到我的 ListView 后,我尝试加载更多元素(此功能工作正常)但是当我滚动到 ListView 底部时,我想加载另外 20 个用户,但它崩溃了。


这是我到目前为止所做的


过滤数据类


private String oldestUserID;


private void fetchData(DataSnapshot dataSnapshot) {


    mList.clear();


    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {


        oldestUserID = dataSnapshot.getKey();

        userPojo = snapshot.getValue(UserPojo.class);

        String email = userPojo.getEmail();

        mList.add(email);

    }

    fetchSuccessListener.onListFetched(true);

}


public void refillListView(ListView listView) {


    listView.setOnScrollListener(new AbsListView.OnScrollListener() {


        @Override

        public void onScrollStateChanged(AbsListView view, int scrollState) {

            currentScrollState = scrollState;

            isScrollCompleted();


        }


        @Override

        public void onScroll(AbsListView view, int firstVisibleItem,

                             int visibleItemCount, int totalItemCount) {

            currentFirstVisibleItem = firstVisibleItem;

            currentVisibleItemCount = visibleItemCount;

            totalItem = totalItemCount;


        }


        private void isScrollCompleted() {

            if (totalItem - currentFirstVisibleItem == currentVisibleItemCount

                    && currentScrollState == SCROLL_STATE_IDLE) {


                mDatabase.orderByKey().startAt(oldestUserID).limitToFirst(20).addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override

                    public void onDataChange(DataSnapshot dataSnapshot) {

                        fetchData(dataSnapshot);

                    }


                    @Override

                    public void onCancelled(DatabaseError databaseError) {


                    }

                });

            }



        }



    });

}

这就是它的工作方式,我使用一个界面来检测我的数据何时完成获取并通知我的适配器填充 ListView,这对我的前 20 个用户工作正常,但是当我滚动到最后时,我希望添加列表新用户(还有 20 个),但我收到了这个崩溃日志


无法使用先前设置的限制调用 limitToLast 查询!


现在,这是我如何调用我的方法以获取数据


主Activity.class


检索数据时填充列表视图的界面(工作正常)


http://img1.mukewang.com/611e28a900017e7e04210425.jpg

Helenr
浏览 134回答 2
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    }});

慕容708150

在这里,我完成了 Firebase-Chat-Pagination。检查这个。它可能对你有帮助。private void loadDataFirst() {&nbsp; &nbsp; mUserRefValueEventListener = mConversationRef.child(mLocation)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .limitToLast(TOTAL_ITEM_EACH_LOAD)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .orderByKey()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addValueEventListener(new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // It should be null when new meesage sent by own&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mLastDataSnapshot = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dataSnapshot.exists()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mChatList.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (DataSnapshot readDataSnapshot : dataSnapshot.getChildren()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChatResponse chatModel = readDataSnapshot.getValue(ChatResponse.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (chatModel != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mChatList.add(chatModel);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // We need First DataSnapshot for pagination&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mLastDataSnapshot == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mLastDataSnapshot = readDataSnapshot;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mView.onFetchMessagesSuccess(mChatList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCancelled(DatabaseError databaseError) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });}&nbsp;public void loadMoreDataPagination() {&nbsp; &nbsp; if (mLastDataSnapshot != null && (mTotalCount == 0 || mView.getChatMessageCount() < mTotalCount)) {&nbsp; &nbsp; &nbsp; &nbsp; mUserRefValueEventListener = mConversationRef.child(mLocation)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .orderByKey()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .endAt(mLastDataSnapshot.getKey())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .limitToLast(TOTAL_ITEM_EACH_LOAD)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addValueEventListener(new ValueEventListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onDataChange(DataSnapshot dataSnapshot) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dataSnapshot.exists()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mChatList.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (DataSnapshot readDataSnapshot : dataSnapshot.getChildren()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChatResponse chatModel = readDataSnapshot.getValue(ChatResponse.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (chatModel != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mChatList.add(chatModel);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // We need Last DataSnapshot for pagination while load more&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mChatList.size() == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mLastDataSnapshot = readDataSnapshot;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // One item duplicates from last list so need to Remove it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mChatList.remove(mChatList.size() - 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mView.onFetchLoadMoreSuccess(mChatList, mTotalCount);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCancelled(DatabaseError databaseError) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mView.hideLoading();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java