我正在使用自定义CursorAdapter从我的SQLite数据库加载文章并将其附加到我的ListView。现在我想在我的应用中使用CWAC EndlessAdapter。我没有找到使用CursorAdapter的示例,仅使用带有ArrayList的demo code of the library和this example。
具体来说:
如果可以的话,如何告诉我的SQLite数据库获取接下来的20个条目?我应该按照this solution?
在EndlessAdapter中需要进行哪些更改才能将其与自定义CursorAdapter一起使用?
是否有任何可用的示例显示使用自己的适配器而不使用ArrayLists?
提前感谢您的帮助!
答案 0 :(得分:3)
所以我没有使用EndlessAdapter库就这样解决了问题:
我的ListFragment实现了AbsListView.OnScrollListener并在我的listview上设置了OnScrollListener,如this post中所述:
currentPage = 0;
listView.setOnScrollListener(this);
然后我添加了这两种方法:
/**
* 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();
}
}
从数据库中获取正确的光标:
/**
* 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;
}