Note that there are some explanatory texts on larger screens.

plurals
  1. POTell Android AsyncTaskLoader to retrieve more data
    primarykey
    data
    text
    <p>Let me start off by saying this is NOT a question about scrolling ListViews. I do not want to know how to tell when a user scrolls to the bottom of a list, so please do not give me answers for that question, or mark this as a duplicate.</p> <p>I am using a class that extends AsyncTaskLoader to populate a ListView with data from a web service.</p> <p>Initially, I load 50 items and everything is working great. I need to know how to tell the Loader to load the next 50 items incrementally. I understand WHERE to do this in the ListView code, but <strong>I can't figure out the best way to tell the Loader that I want to load more data</strong> without resetting it and loading everything again.</p> <p>Again, to clarify, the issue I'm trying to solve here is just notifying the loader that more data needs to be loaded. It already knows how to load more data when loadInBackground() is called a second time, and the ListView already knows where/when to notify the Loader, the question is just <em>how</em>.</p> <p>Some of the relevant code:</p> <pre><code>@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); m_adapter = new SearchAdapter(getActivity()); setListAdapter(m_adapter); // if the loader doesn't already exist, one will be created // otherwise the existing loader is reused so we don't have // to worry about orientation and other configuration changes getLoaderManager().initLoader(SEARCH_LOADER_ID, null, this); } @Override public Loader&lt;List&lt;Result&gt;&gt; onCreateLoader(int id, Bundle args) { String query = args != null ? args.getString(QUERY_KEY) : ""; return new SearchLoader(getActivity(), query); } private class SearchAdapter extends ArrayAdapter&lt;Result&gt; { // ... @Override public View getView(int position, View convertView, ViewGroup parent) { // ... if (position == getCount() - 2) // TODO: Need to notify Loader here // ... } } private static class SearchLoader extends OurAsyncTaskLoader&lt;List&lt;Result&gt;&gt; { public SearchLoader(Context context, String query) { super(context); m_query = query; m_data = Lists.newArrayList(); m_loadedAllResults = false; } @Override public List&lt;Result&gt; loadInBackground() { if (m_loadedAllResults) return m_data; // the Loader implementation does a == check rather than a .equals() check // on the data, so we need this to be a new List so that it will know we have // new data m_data = Lists.newArrayList(m_data); MyWebService service = new MyWebService(); List&lt;Result&gt; results = service.getResults(m_query, m_data.size(), COUNT); service.close(); if (results == null) return null; if (results.size() &lt; COUNT) m_loadedAllResults = true; for (Result result : results) m_data.add(result) return m_data; } private static final int COUNT = 50; private final String m_query; private boolean m_loadedAllResults; private List&lt;Result&gt; m_data; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload