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.