Listview data loading and listview Data Loading
Effect
The data loading of listview mainly uses the Scroll listener to determine whether information needs to be loaded at its location. If the information needs to be loaded, the view of the loaded data is displayed first, and then the data is loaded, after the data is loaded, the view of the loaded data is invisible. If the data bit is null when the data is loaded, the view is removed.
ListView. addFooterView (moreView); // Add the bottom view (loading data in...) before setAdapter. Otherwise, an error is returned.
ListView. removeFooterView (moreView); // remove the bottom (same as above)
The previous Demo below is from the Internet. It is quite practical to write a blog post to record this function.
Public class MainActivity extends Activity implements OnScrollListener {private static final String TAG = "MainActivity"; private ListView listView; private View moreView; // load more pages private SimpleAdapter adapter; private ArrayList <HashMap <String, String> listData; private int lastItem; private int count; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setConte NtView (R. layout. activity_main); listView = (ListView) findViewById (R. id. listView); moreView = getLayoutInflater (). inflate (R. layout. load, null); listData = new ArrayList <HashMap <String, String> (); prepareData (); // prepare Data count = listData. size (); adapter = new SimpleAdapter (this, listData, R. layout. item, new String [] {"itemText"}, new int [] {R. id. itemText}); listView. addFooterView (moreView); // Add the bottom view Must be added before setAdapter; otherwise, an error is reported. ListView. setAdapter (adapter); // set adapterlistView. setOnScrollListener (this); // sets the listview rolling event} private void prepareData () {// prepare data for (int I = 0; I <10; I ++) {HashMap <String, String> map = new HashMap <String, String> (); map. put ("itemText", "Test Data" + I); listData. add (map) ;}} private void loadMoreData () {// load more data count = adapter. getCount (); for (int I = count; I <count + 5; I ++) {HashMap <String, Str Ing> map = new HashMap <String, String> (); map. put ("itemText", "Test Data" + I); listData. add (map);} count = listData. size () ;}@ Overridepublic void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {Log. I (TAG, "firstVisibleItem =" + firstVisibleItem + "\ nvisibleItemCount =" + visibleItemCount + "\ ntotalItemCount" + totalItemCount); lastItem = firstVisibleItem + visibleItem Count-1; // minus 1 is because addFooterView} @ Overridepublic void onScrollStateChanged (AbsListView view, int scrollState) {Log is added. I (TAG, "scrollState =" + scrollState); // when the number of the last item equals the total number of data, update System. out. println ("lastItem:" + lastItem + "\ n" + "count" + count); if (lastItem = count & scrollState = this. SCROLL_STATE_IDLE) {Log. I (TAG, "Pull to the bottom"); moreView. setVisibility (view. VISIBLE); mHandler. sendEmptyMess Age (0); // send to Handler and execute loading data} // declare Handlerprivate Handler mHandler = new Handler () {public void handleMessage (android. OS. message msg) {switch (msg. what) {case 0: try {Thread. sleep (400);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();} loadMoreData (); // load more data. You can load the adapter asynchronously here. notifyDataSetChanged (); // notifyDataSetChanged () after modifying the array bound to the adapter, you do not need to refresh the Activity to notify Acti Worker updates the ListView. MoreView. setVisibility (View. GONE); if (count> 30) {Toast. makeText (MainActivity. this, "wood has more data! ", 3000 ). show (); listView. removeFooterView (moreView); // remove the bottom} Log. I (TAG, "load more data"); break ;}};};}
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" /></RelativeLayout>
Zookeeper
Add data to listview
This is a little different from the dataGridView. The dataGridView is just like an Excel file. You can directly add, delete, and modify data. rows [I] locates rows and cells [j] locates columns. While listview and the dataGridView are the same, they must be pre-edited columns, while listview does not need to be pre-bound to the columns in the data table, but is read through the sqldatareader object.
Here we mainly write some important code:
While (datareader. read ())
{
String name = (string) datareader ["name"]; // assume that the data table contains the name field, as shown in the preceding figure.
String sex = (string) datareader ["sex"];
// Create a list view item
ListViewItems lvi = new listViewsItems (name );
// Add the created list view item to the List View
Lv. Items. Add (lvi); // Where lv is the name of the List listview
// Add a subitem to the List View
Lvi. SubItems. AddRange (new string [] {sex });
}
Datareader. Close (); // Close the dataReader object
Upload listview data repeatedly in android Development
Every time you load data, Have you cleared the list of saved data? Remember to clear when reload data