1

I am using a custom CursorAdapter to load articles from my SQLite database and attach it to my ListView. Now I want to use CWAC EndlessAdapter in my app. I found no example using a CursorAdapter just the demo code of the library and this example with an ArrayList.

So to be specific:

  1. How can I tell my SQLite database to get i.e the next 20 entries if available? Should I do this as explained in this solution?

  2. Which changes are necessary in the EndlessAdapter to use it with a custom CursorAdapter?

Are there any examples available that show the use of a own adapter not with ArrayLists?

Thanks in advance for your help!

Community
  • 1
  • 1
Mokkapps
  • 2,028
  • 9
  • 42
  • 67
  • 1
    The CWAC-Endless project is retired. It never supported `CursorAdapter`. – CommonsWare Dec 09 '14 at 15:28
  • Ok thanks for the information. Do you know an alternative library or do I have to code it by myself? – Mokkapps Dec 09 '14 at 15:38
  • 1
    I have not gone looking. IMHO, for local storage access, an "endless" pattern is inappropriate. If your tables may be too large to retrieve in one gulp, you should be focusing your UI on search. – CommonsWare Dec 09 '14 at 15:40
  • I solved the problem without using the EndlessAdapter library and added the solution to this post. – Mokkapps Dec 09 '14 at 18:40
  • As far as I understand the CursorAdapter, it is sort of an endless adapter in itself, as it keeps a 'window' of data and only fetches the rows currently visible (and some before and after). – Ridcully Dec 09 '14 at 18:40

1 Answers1

3

So I solved the problem this way without using the EndlessAdapter library:

My ListFragment implements AbsListView.OnScrollListener and set a OnScrollListener on my listview as described in this post:

currentPage = 0;
listView.setOnScrollListener(this);

Then I added this two methods:

/**
 * Method to detect scroll on listview
 */
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount) {
    // Leave this empty
}

/**
 * Method to detect if scrolled to end of listview
 */
@Override
public void onScrollStateChanged(AbsListView listView, int scrollState) {
    if (scrollState == SCROLL_STATE_IDLE) {
        if (listView.getLastVisiblePosition() >= listView.getCount() - 1 - threshold) {
            Log.d(TAG, "Scrolled to end of list, load more articles");
            currentPage++;
            Log.d(TAG, "CurrentPage for EndlessAdapter:" + currentPage);
            // Load more articles
            loadMoreArticles(currentPage);
        }
    }
}

/**
 * Method to load more articles if scrolled to end of listview
 */
private void loadMoreArticles(int currentPage) {
    int from = currentPage * 10;
    int to = currentPage * 20;
    if(dba.getArticlesCount() < to){
        Log.d(TAG, "Not enough articles available! ArticleCount: "+ dba.getArticlesCount() + " ,tried to load " + to);
        listView.removeFooterView(footerView);
    }else{
        Log.d(TAG, "Load the next articles, from " + from + " to " + to);
        articleCursor = dba.getXArticles(0, to);
        adapter.changeCursor(articleCursor);
        adapter.notifyDataSetChanged();
    }
}

To get the correct cursor from database:

/**
 * Reading all rows from database
 */

public Cursor getXArticles(int from, int to) {

String selectQuery = "SELECT  * FROM " + TABLE_RSS
        + " ORDER BY date DESC LIMIT " + from + ", " + to;
Log.d(TAG, "getXArticle SQL: " + selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
return cursor;
}
Community
  • 1
  • 1
Mokkapps
  • 2,028
  • 9
  • 42
  • 67