Android _ develop listview to load data by PAGE

Source: Internet
Author: User

In Android Application Development, It is very common to use the listview component to display data. When an application needs to display a lot of data, generally, all data is not displayed once. Instead, data is displayed by page. I personally think this will provide a better user experience. Therefore, many applications load data in batches to obtain user data. For example, the Weibo client may automatically load the next page of data when the user slides to the bottom of the list, or may place a "view more" button at the bottom. After the user clicks it, the next page of data is loaded.

The following uses a demo to demonstrate how to implement the listview function: This demo adds "view more..." at the bottom of the listview list... "button to load the paging data of the News (simulating the news client. At the same time, it is limited to 10 records each time, but after the data is fully loaded, the view "view more..." at the bottom of the listview list will be deleted. Assume that the total number of loaded data records is 38. Let's take a look at the program structure of the demo project:


The news. Java class in the package com. andyidea. Bean is the news entity class, And the paginationlistviewactivity. Java class in the package com. andyidea. listview is used to display the listview list. Layout contains three layout files: list_item.xml, loadmore. XML, and Main. xml. The following are the source code:

List_item.xml source code in layout:

<span style="font-size:13px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical">  <TextView     android:id="@+id/newstitle"     android:layout_width="fill_parent"     android:layout_height="wrap_content"/>  <TextView     android:id="@+id/newscontent"     android:layout_width="fill_parent"     android:layout_height="wrap_content"/></LinearLayout></span>

Loadmore. xml source code in layout:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <button Android: Id = "@ + ID/loadmorebutton" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "view more... "/> </linearlayout>

Main. xml source code in layout:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <ListView       android:id="@+id/lvNews"       android:layout_width="fill_parent"       android:layout_height="wrap_content"/></LinearLayou

Source code of the news. Java class in the com. andyidea. Bean package:

Package COM. andyidea. bean;/*** news entity class * @ author Andy. chen * @ mail Chenjunjun.ZJ@gmail.com **/public class news {private String title; // Title private string content; // content Public String gettitle () {return title ;} public void settitle (String title) {This. title = title;} Public String getcontent () {return content;} public void setcontent (string content) {This. content = content ;}}

Source code of paginationlistviewactivity. Java class in COM. andyidea. listview:

Package COM. andyidea. listview; import Java. util. arraylist; import Java. util. list; import COM. andyidea. bean. news; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. util. log; import android. view. view; import android. view. viewgroup; import android. widget. abslistview; import android. widget. abslistview. onscrolllistener; import android. widget. baseadapter; import android. Widget. button; import android. widget. listview; import android. widget. textview; import android. widget. toast; public class paginationlistviewactivity extends activity implements onscrolllistener {private listview; private int visiblelastindex = 0; // The final visual index private int visibleitemcount; // total number of visible items in the current window private int datasize = 38; // Number of simulated datasets private paginationadapter; private view loadmorev Iew; private button loadmorebutton; private handler = new handler ();/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); loadmoreview = getlayoutinflater (). inflate (R. layout. loadmore, null); loadmorebutton = (button) loadmoreview. findviewbyid (R. id. loadmorebutton); load Morebutton. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {loadmorebutton. settext ("loading... "); // set the button text handler. postdelayed (New runnable () {@ overridepublic void run () {loadmoredata (); adapter. notifydatasetchanged (); loadmorebutton. settext ("view more... "); // restore button text }}, 2000) ;}); listview = (listview) findviewbyid (R. id. lvnews); listview. addfooterview (loadmoreview); // sets the bottom of the list. Partial View initializeadapter (); listview. setadapter (adapter); listview. setonscrolllistener (this) ;}@ overridepublic void onscrollstatechanged (abslistview view, int scrollstate) {int itemslastindex = adapter. getcount ()-1; // The index int lastindex = itemslastindex + 1 for the last dataset; If (scrollstate = onscrolllistener. scroll_state_idle & visiblelastindex = lastindex) {// if it is automatically loaded, you can place the code for Asynchronously loading data here }}@ overridepublic void Onscroll (abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {This. visibleitemcount = visibleitemcount; visiblelastindex = firstvisibleitem + visibleitemcount-1; log. E ("============================= ", "=================="); log. E ("firstvisibleitem =", firstvisibleitem + ""); log. E ("visibleitemcount =", visibleitemcount + ""); log. E ("totalitemcount =", totalitemcount + ""); log. E ("============================= ", "===================="); // if all the record options are equal to the number of data sets, remove the view at the bottom of the list if (totalitemcount = datasize + 1) {listview. removefooterview (loadmoreview); toast. maketext (this, "data is fully loaded! ", Toast. length_long ). show () ;}}/*** initialize the listview adapter */private void initializeadapter () {list <News> News = new arraylist <News> (); for (INT I = 1; I <= 10; I ++) {news items = new news (); items. settitle ("title" + I); items. setcontent ("this is news content" + I); news. add (items);} adapter = new paginationadapter (News);}/*** load more data */private void loadmoredata () {int COUNT = adapter. getcount (); If (count + 10 <= datasize) {for (INT I = count + 1; I <= count + 10; I ++) {news item = new news (); item. settitle ("title" + I); item. setcontent ("this is news content" + I); adapter. addnewsitem (item) ;}} else {for (INT I = count + 1; I <= datasize; I ++) {news item = new news (); item. settitle ("title" + I); item. setcontent ("this is news content" + I); adapter. addnewsitem (item) ;}} class paginationadapter extends baseadapter {list <News> newsitems; Public paginationadapter (list <News> newsitems) {This. newsitems = newsitems;} @ overridepublic int getcount () {return newsitems. size () ;}@ overridepublic object getitem (INT position) {return newsitems. get (position) ;}@ overridepublic long getitemid (INT position) {return position ;}@ overridepublic view getview (INT position, view, viewgroup parent) {If (view = NULL) {view = getlayoutinflater (). inflate (R. layout. list_item, null);} // news title textview tvtitle = (textview) view. findviewbyid (R. id. newstitle); tvtitle. settext (newsitems. get (position ). gettitle (); // news content textview tvcontent = (textview) view. findviewbyid (R. id. newscontent); tvcontent. settext (newsitems. get (position ). getcontent (); Return view;}/*** Add a data list item * @ Param newsitem */Public void addnewsitem (News newsitem) {newsitems. add (newsitem );}}}

Finally, the result of running the program is as follows:

Through the above, when we click "view more... "button, 10 records will be loaded. After all records are loaded, the bottom view of listview will be removed.

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.