Reputation: 2028
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:
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?
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!
Upvotes: 1
Views: 946
Reputation: 2028
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;
}
Upvotes: 3