ListView paging data loading for Android

Source: Internet
Author: User

1. The effect is as follows:



The example is as follows: The add data button can be changed to a progress bar because there is no data, so I add a button to the database for testing. Generally, it takes some time to pull data from the server, so you can get a progress bar to prompt the user:

When you click the Load button, read the data from the database and append the read data to the original dataset.


Package com. exampleandroid. xiong. listviewpages; public class News {private String title; private int id;/***** @ return News title */public String getTitle () {return title ;} /***** @ param title * set the news title */public void setTtitle (String title) {this. title = title;}/***** @ return news id */public int getId () {return id ;} /***** @ param id * set the news id */public void setId (int id) {this. id = id ;}}


Package com. exampleandroid. xiong. listviewpages; import java. util. arrayList; import java. util. list; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; public class GetNews {/***** @ param page * Number of pages to be loaded 5 records per page * @ param dbnews * SQLiteOpenHelper subclass * @ return returned news loaded data */public list
 
  
GetListNews (int page, DbSqliteNews dbnews) {List
  
   
List = new ArrayList
   
    
(); String SQL = "select * from tb_newstile where news_id not in (select news_id from tb_newstile LIMIT" + 5 * (page-1) + ") LIMIT" + 5 * page; cursor cursor = dbnews. getReadableDatabase (). rawQuery (SQL, null); while (cursor. moveToNext () {News news = new News (); news. setTtitle (cursor. getString (1); news. setId (cursor. getInt (0); list. add (news);} cursor. close (); return list;}/*** insert 100 pieces of data for testing ** @ param d Bnews * SQLiteOpenHelper subclass */public void insertData (DbSqliteNews dbnews) {SQLiteDatabase datas = dbnews. getWritableDatabase (); datas. beginTransaction (); try {for (int I = 0; I <100; I ++) Export datas.exe cSQL ("insert into tb_newstile values (?,?) ", New Object [] {I," news title "+ I});} datas. setTransactionSuccessful ();} catch (Exception e) {System. out. println ("data insertion failed! "); E. printStackTrace () ;}finally {datas. endTransaction ();}}}
   
  
 


Package com. exampleandroid. xiong. listviewpages; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteDatabase. cursorFactory; import android. database. sqlite. SQLiteOpenHelper; public class DbSqliteNews extends SQLiteOpenHelper {public DbSqliteNews (Context context, String name, CursorFactory factory, int version) {super (context, name, factory, version );} // create a database table @ Overridepublic void onCreate (SQLiteDatabase db) mongodb.exe cSQL ("create table tb_newstile (news_id integer, news_title varchar (100 ))");} @ Overridepublic void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){}}


Package com. exampleandroid. xiong. listviewpages; import java. util. list; import android. app. activity; import android. OS. bundle; import android. view. layoutInflater; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. button; import android. widget. imageView; import android. widget. listView; import android. widget. textView; public class MainActivity extends Activity {private ListView newShow_list; private Button loadmore, adddata; // private List of data loaded by ListView
 
  
Shownews; private GetNews getnews; private DbSqliteNews dbnews; // The number of loaded pages private int pagecount = 1; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); newShow_list = (ListView) findViewById (R. id. newsShow_list); loadmore = (Button) findViewById (R. id. loadmore_bt); adddata = (Button) findViewById (R. id. adddata); dbnews = new DbSqliteNews (this, "new. db ", null, 1); getnews = new GetNews (); final ListAdpaterNews listadpter = new ListAdpaterNews (); // This button only adds test data to adddata. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// insert data getnews. insertData (dbnews); // obtain the data shownews = getnews. getListNews (pagecount, dbnews); // display data newShow_list.setAdapter (listadpter) ;}}); loadmore. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {pagecount ++; // append data to the original set shownews. addAll (getnews. getListNews (pagecount, dbnews); // refresh the data listadpter. notifyDataSetInvalidated () ;}}); // shownews = getnews. getListNews (pagecount, dbnews); newShow_list.setAdapter (listadpter);} class ListAdpaterNews extends BaseAdapter {@ Overridepublic int getCount () {return pagecount * 5;} @ Overridepublic Object getItem (position) {return position ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {View view = LayoutInflater. from (MainActivity. this ). inflate (R. layout. news_title, null); TextView txttitle = (TextView) view.findViewById(R.id.txt _ title); ImageView images = (ImageView) view. findViewById (R. id. showimage_title); images. setBackgroundResource (R. drawable. n_me_l); txttitle. setText (shownews. get (position ). getTitle (); return view; }}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
 

     
      
      
          
           
       
  
 

 
     
      
  
 



Source code download: http://download.csdn.net/detail/x605940745/6800461

Reprinted please indicate the source: http://blog.csdn.net/x605940745

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.