ListView dynamically loads data

Source: Internet
Author: User

When the listview needs to load too much data, if it is loaded at a time, the speed will be quite slow, affecting the user experience. At this time, it needs to dynamically load data, that is, each time a fixed length of data is loaded, android market listview adopts this method to make loading look smooth and fast, which helps improve user experience.
First, to achieve dynamic loading, you must first obtain a fixed length of data at the position after the data obtained last time, you can use the "select * from TableName LIMIT m OFFSET n" statement. m indicates the length of the data to be obtained, and n indicates the data OFFSET.
Then, you need to override the onScroll method of listview in the Activity and add judgment conditions to onScroll. When the data loaded at a time is displayed and slides to the bottom, you need to obtain the data again, you can enable a Thread in the UI Thread to load data. An attempt is prompted when loading data.
Finally, refresh the ListView after the data loading thread finishes processing.
The main steps for dynamic loading are as follows:
// Method for loading data
 
Private void fillAdapter (int count, int begin ){
// TODO Auto-generated method stub
SQLiteOpenHelper mSQLiteOpenHelper = new SQLiteOpenHelper (
This );
Cursor c = mSQLiteOpenHelper. getDynamicListViewData (count, offset );
 
}
 
// Listen to the onScroll method of listview
 
OnScrollListener listScroll = new OnScrollListener (){
 
@ Override
Public void onScrollStateChanged (AbsListView view, int scrollState ){
// TODO Auto-generated method stub
}
 
@ Override
Public void onScroll (AbsListView view, int firstVisibleItem,
Int visibleItemCount, int totalItemCount ){
If (isLoading) // when loading, do not trigger the event during activity execution.
Return;
If (firstVisibleItem + visibleItemCount = totalItemCount ){
// Open a thread to obtain data
If (totalItemCount <= totalItemsCount ){
Integer [] params = new Integer [] {25, currentItemCount };
 
MAsynchTask = new AsynchTask ();
MAsynchTask.exe cute (params );
} Else {
Toast. makeText (EventTracker. this, "there is no data! ",
Toast. LENGTH_SHORT). show ();
}
}
}
};
 
 
// Use AsyncTask to conveniently implement multi-thread management, making the loading effect smoother
 
Private class AsynchTask extends AsyncTask <Integer, Void, Void> {
@ Override
/** It will be executed immediately after the onPreExecute method is executed. This method runs in the background thread.
* Here, we will be mainly responsible for performing time-consuming background computing. You can call the publishProgress method.
* To update the real-time task progress. This method is an abstract method and must be implemented by sub-classes. */
Protected Void doInBackground (Integer... params ){
FillAdapter (params [0], params [1]);
Return null;
}
 
@ Override
/** After doInBackground is executed, the onPostExecute method will be called by the UI thread,
* The background computing result is passed to the UI thread through this method.
**/
Protected void onPreExecute (){
 
// LoadingView is a custom dialog, used to prompt that the user is loading
MEventListView. addFooterView (loadingView );
IsLoading = true;
}
 
 
@ Override
/** After doInBackground is executed, the onPostExecute method will be called by the UI thread,
* The background computing result is passed to the UI thread through this method.
**/
Protected void onPostExecute (Void result ){
BindAdapter ();
MListView. removeFooterView (loadingView );
MListView. setSelection (currentItemCount );
CurrentItemCount + = 25; // each time you load 25 columns, the offset increases by 25
IsLoading = false;
}
}
 
The method in the database is as follows:
Public Cursor getDynamicListViewData (int count, int offset ){
Return getReadableDatabase (). rawQuery (
"Select * from TableName limit" + count + "offset" + offset,
Null );
}
 
This article is from the "HDDevTeam" blog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.