I have DB table with ~15,000 rows which I want to display in the listview. I want to display first 100 and when the user scrolls down to the last item the next 100 should be loaded (an so on…). I have implemented on OnScrollListener() which calls AsyncTask responsible for loading more items. The problem I’ve got is that my SimpleCursorAdapter is not updated after more rows are added to the cursor. I have tried adapter.notifyDataSetChanged(); but that doesn’t do anything.
This is the list listener:
myListView.setOnScrollListener(new OnScrollListener(){
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int lastInScreen = firstVisibleItem + visibleItemCount;
if(resultCursor != null){
if(lastInScreen == totalItemCount && isLoadingMore == false){
isLoadingMore = true;
loadedPage ++;
new LoadBooks().execute();
}
}
}
public void onScrollStateChanged(AbsListView view, int scrollState) {}
});
This is my AsyncTask class:
private class LoadBooks extends AsyncTask<String, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(FullIndex.this);
@Override
protected void onPreExecute() {
this.dialog.setMessage("Loading books...");
this.dialog.show();
}
@Override
protected Void doInBackground(String... arg0) {
try{
resultCursor = dbHelper.fetchBooks(0, loadedPage * LIMIT_RESULTS);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(final Void unused){
if(resultCursor != null){
if(adapter == null){
startManagingCursor(resultCursor);
String[] from = new String[]{"name"};
int[] to = new int[]{R.id.book_item_tbx};
getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
adapter = new SimpleCursorAdapter(FullIndex.this, R.layout.book_item, resultCursor, from, to);
setListAdapter(adp);
}else{
adapter.notifyDataSetChanged();
}
}
if(dialog != null && dialog.isShowing()){
dialog.dismiss();
}
isLoadingMore = false;
}
}
New rows are added to resultCursor but the list is not updated, what am I missing?
I will post a code which I have used to populated 10 records each on scroll event.
fetchHistory(int count)is the method I have used to set the values fortotalHistoryItemCount&checkInCheckOutHistoryList.Hope this would help.