1

I'm using the absurdly simple EndlessAdapter to load paged data from Facebook. In my project the EndlessAdapter to wrap a custom BaseAdapter. I'm also using a PullToRefreshListView which is from this library. Everything loads fine to start up, I get the data for the next page load, I scroll to the bottom of the list, the next page of data loads and populates, but when I scroll to the bottom again, nothing happens. I have log checks for the methods and chacheInBackground() doesn't run at all after the first successful page load.

I thought it might have to do with the "pull to refresh" library and the adapter not being able to detect the ListView position or something, so I got rid of the "pull to refresh" and still had the same issue.

Here is the relevant code:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    view = inflater.inflate(R.layout.news_feed, container, false);
    if(appState.getNewsFeed()==null){
        refreshListener.onRefresh(refreshFeedList);
        Log.i("NewsFeed", "executing Request");
        if(appState.lastFeed().contains("lastFeed") && feedAdapter == null){
            try {
                String lastFeedString = appState.lastFeed().getString("lastFeed", "null");
                JSONObject lastFeedObject = new JSONObject(lastFeedString);
                JSONArray lastFeedArray;
                lastFeedArray = lastFeedObject.getJSONArray("data");
                appState.setNewsFeed(lastFeedArray);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            updateFeed();
        }
    }
    else{
        Log.i("NewsFeed", "recreating adapters from existing data");
        updateFeed();
    }

    return view;
}

public void updateFeed(){
    refreshFeedList = (PullToRefreshListView) view.findViewById(R.id.feedList);
    feedList = refreshFeedList.getRefreshableView();
    feedList.setDividerHeight(0);
    feedAdapter = new FeedAdapter(getActivity(), appState.getNewsFeed());
    endlessAdapter = new EndlessFeed(feedAdapter);
    refreshFeedList.setAdapter(endlessAdapter);
    refreshFeedList.setOnRefreshListener(refreshListener);
    refreshFeedList.setOnItemClickListener(itemClickListener);
}

public class EndlessFeed extends EndlessAdapter {

    @Override
    protected View getPendingView(ViewGroup parent) {
        Log.i("NewsFeed", "running getPendingView()");
        View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_view, null);
        return (row);
    }

    public EndlessFeed(BaseAdapter wrapped) {
        super(wrapped);
    }

    @Override
    protected boolean cacheInBackground() throws Exception {
        Log.i("NewsFeed", "running cacheInBackground()");
        nextPageRequest.setCallback(nextPageCallback);
        nextPageRequest.executeAndWait();
        return false;
    }

    @Override
    protected void appendCachedData() {
        try {
            appState.addToNewsFeed(requestResponse);
        } catch (JSONException e) {
            e.printStackTrace();
        }
//          onDataReady();
    }
}

The JSONArray sent into the adapter is saved as a global variable with getter and setter methods in an Application subclass.

Since I recreate the adapters and call setAdapter() in the onCreateView() method, if I change to a different fragment (via one of the clickable things in the ListView) and then navigate back to this fragment, it'll load another page of data. Any ideas? Thanks.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Wenger
  • 989
  • 2
  • 12
  • 35

1 Answers1

3

The answer is clearly in Commonsware's documentation on how the EndlessAdapter is intented to work:

Your EndlessAdapter subclass also needs to implement cacheInBackground(). (...) This method returns a boolean, which needs to be true if there is more data yet to be fetched, false otherwise.

Since you're always returning false from cacheInBackground(), it will never get run again after the first time. Hence it only loads more data once when you scroll to the bottom.

MH.
  • 45,303
  • 10
  • 103
  • 116
  • Ahhh thanks. I took that statement to mean that you have to return `false` if that given page is done loading, not if the entire thing never has to load more data again. – Wenger Feb 28 '13 at 04:32