我正在使用自定义 CursorAdapter 从我的 SQLite 数据库加载文章并将其附加到我的 ListView。现在我想使用 CWAC EndlessAdapter在我的应用程序中。我没有找到使用 CursorAdapter 的例子,只是 demo code of the library和 this example使用 ArrayList。
具体来说:
我如何告诉我的 SQLite 数据库获取下 20 个条目(如果可用)?我应该按照 this solution 中的说明执行此操作吗? ?
要将 EndlessAdapter 与自定义 CursorAdapter 一起使用,需要进行哪些更改?
是否有可用的示例显示使用自己的适配器而不是 ArrayLists?
预先感谢您的帮助!
最佳答案
所以我在不使用 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;
}
关于android - 如何在带有自定义 CursorAdapter 的 ListView 中使用 EndlessAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27382523/