I am trying to implement endless list in an application where you get first 25 results from webservice and when it goes to the bottom of the list it loads the next 25 and so on until it reaches the total results. But I am lost at how does the endless list adapter knows when I reached the bottom of the list ? I looked at the DemoAdapter and EndlessAdapterDemo example from cwac but cant figure it out.. or it has to do with my Activity is not a ListActivity (I think not)? The first 25 results are loaded in the list but after that I need to change the call paramterers to call the next 25 I don't use asyntask (using something else ) for getting the results or I misinterpreted the guide for using asnytask in cacheInBackground ? but when and how comes the endlessadapter in play ? Here is my implementation of the adapter:
public class SearchResultEndlessAdapter extends EndlessAdapter {
private static final String TAG = SearchResultEndlessAdapter.class.getSimpleName();
private RotateAnimation rotate = null;
private LayoutInflater inflater;
private View pendingView = null;
private List<KeywordSearchResults> newKsearchResultList;
private int limit = 0;
public SearchResultEndlessAdapter(SearchResultListAdapter adapter, Context ctx) {
super(adapter);
this.inflater = LayoutInflater.from(ctx);
rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(600);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
}
/**
* Set the new list that is loaded from the webservice
*
* @param newKsearchResultList
* List<KeywordSearchResults>
*/
public void setNewData(List<KeywordSearchResults> newKsearchResultList) {
this.newKsearchResultList = new ArrayList<KeywordSearchResults>(newKsearchResultList);
}
/**
* The value of total results
*
* @param limit
* int
*/
public void setLimit(int limit) {
this.limit = limit;
}
@Override
protected void appendCachedData() {
LogService.log(TAG, "appendCachedData");
if (getWrappedAdapter().getCount() < limit) {
SearchResultListAdapter srListAdapter = (SearchResultListAdapter) getWrappedAdapter();
for (KeywordSearchResults kws : newKsearchResultList) {
srListAdapter.add(kws);
}
}
}
@Override
protected boolean cacheInBackground() throws Exception {
LogService.log(TAG, "cacheInBackground");
return (getWrappedAdapter().getCount() < limit);
}
@Override
protected View getPendingView(ViewGroup parent) {
LogService.log(TAG, "getPendingView");
View row = inflater.inflate(R.layout.row, null);
pendingView = row.findViewById(android.R.id.text1);
pendingView.setVisibility(View.GONE);
pendingView = row.findViewById(R.id.throbber);
pendingView.setVisibility(View.VISIBLE);
pendingView.startAnimation(rotate);
startProgressAnimation();
return (row);
}
public void startProgressAnimation() {
if (pendingView != null) {
pendingView.startAnimation(rotate);
}
}
}
And parts from myActivity that extends ActionBarActivity:
//in oncreate
searchResultEndlessAdapter = new SearchResultEndlessAdapter(searchResultadapter,this);
searchResultEndlessAdapter.setRunInBackground(false);
mListView.setAdapter(searchResultEndlessAdapter);
mListView.setOnItemClickListener(this);
mListView.setOnScrollListener(this);
//callbacks of scroll
@Override
public void onScroll(AbsListView view, int firstVisible, int visibleCount, int totalCount) {
//something needs to be done here with endlessAdapter ?
}
@Override
public void onScrollStateChanged(AbsListView arg0, int arg1) {
}