0

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) {

}
cesztoszule
  • 286
  • 3
  • 12
  • 1
    Please note that I have retired that project. – CommonsWare Mar 12 '14 at 16:24
  • I understand.. so then can't help how to start it ? I think I am missing something very simple.. – cesztoszule Mar 12 '14 at 16:38
  • @CommonsWare Incidentally: I want to implement the endless listview pattern and couldn't find the reason behind the retirement. Has the project been replaced by some official Google solution or just reached a stable, "nothing else to add" status? Do you still consider it the recommended approach for the pattern? – Piovezan Feb 27 '15 at 12:21
  • "Has the project been replaced by some official Google solution" -- not that I am aware of. "Do you still consider it the recommended approach for the pattern?" -- I do not recommend the pattern. – CommonsWare Feb 27 '15 at 12:42
  • @CommonsWare Thanks. I think the pattern is okay in my case (large remote list to be fetched in small batches by the user) but please feel free to recommend a different pattern for this case. – Piovezan Feb 28 '15 at 02:37

2 Answers2

0

Well couldn't make it work with cwacs adapter.. made it normally.

@Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        int loadedItems = firstVisibleItem + visibleItemCount;

        if ((loadedItems == totalItemCount) && !isloading && totalItemCount > 1) {
            if (!isloading) {
                isloading = true;
                if (!((start - 1) == total)) {
                    // call ws
                    search();
                }
            }
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView arg0, int arg1) {

    }
cesztoszule
  • 286
  • 3
  • 12
0

You can also use EndlessAdapter with ActionBarActivity. All you need to do is change your adapter in Activity which extends ActionBarActivity to SearchResultEndlessAdapter you made. Make sure you are doing constructors properly.

For example this is in my Activity that extends ActionBarActivity:

                       ...

                    if (adapter == null) {
                        ArrayList<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();

                        items.addAll(questionsList);

                        adapter = new CustomAdapter(this, items);
                      }
                      else {
                        adapter.startProgressAnimation();
                      }

                      listView.setAdapter(adapter);

                      ...
Mustafa Berkay Mutlu
  • 1,929
  • 1
  • 25
  • 37