Reputation: 627
I have a database with about 12000 records which I want to load in a ListView. I'm using BaseAdapter. When I load items in ListView, It's taking so long.
Is there any way to reduce this time, for example I have seen an app that loads only limited numbers of items until the scroll bar reaches the end of ListView, then it loads again more items.
Upvotes: 1
Views: 8232
Reputation: 304
You should set onScrollListener in your list and keep a track of the visibility of the items and the offset. I show you an example here:
// Adapter for the custom list
adapter = new Adapter(this, activityList);
setListAdapter(adapter);
registerForContextMenu(getListView());
getListView().setOnScrollListener(new OnScrollListener(){
public void onScroll(AbsListView lw, final int firstVisibleItem,
final int visibleItemCount, final int totalItemCount) {
switch(lw.getId()) {
case android.R.id.list:
// Make your calculation stuff here. You have all your
// needed info from the parameters of this function.
// Sample calculation to determine if the last
// item is fully visible.
final int lastItem = firstVisibleItem + visibleItemCount;
if(lastItem == totalItemCount) {
// Last item is fully visible.
Log.i("a", "last item fully visible...");
try {
if(offset > 0){
int newLimit;
int oldOffset = offset;
if(offset >= limit){
newLimit = limit;
offset = offset - limit;
}
else{
newLimit = length;
offset = 0;
}
for (int i=0; i < newLimit; i++)
{
JSONObject item = jFeed.getJSONObject(i + length - oldOffset);
// Pulling items from the array
// Get list info
String sInfo = item.getString(TAG_INFO);
Log.i(MainActivity.class.getName(), "Info: " + sInfo);
// Populate the dynamic custom list
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_INFO, sInfo);
activityList.add(map);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if(scrollState == 0)
Log.i("a", "scrolling stopped...");
}
});
Upvotes: 0
Reputation: 3735
This may be useful, a ListView
with load More button:
http://www.androidhive.info/2012/03/android-listview-with-load-more-button/
You can also take a look here:
How can I implement paging in listview in android?
Upvotes: 1