欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现简单的分批加载ListView

程序员文章站 2024-02-25 21:13:27
每次滑动至底端,从数据库中获取10条数据,并加载于listview中 数据库 package com.example.listviewbatchloadin...

每次滑动至底端,从数据库中获取10条数据,并加载于listview中

数据库

package com.example.listviewbatchloading; 
 
import android.content.context; 
import android.database.sqlite.sqlitedatabase; 
import android.database.sqlite.sqlitedatabase.cursorfactory; 
import android.database.sqlite.sqliteopenhelper; 
 
public class persondb extends sqliteopenhelper { 
 
  public persondb(context context) { 
    super(context, "creature", null, 1); 
    // todo auto-generated constructor stub 
  } 
 
  @override 
  public void oncreate(sqlitedatabase db) { 
    // todo auto-generated method stub 
    db.execsql("create table people(_id integer primary key autoincrement,name char,number char)"); 
  } 
 
  @override 
  public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { 
    // todo auto-generated method stub 
    db.execsql("drop table people"); 
    oncreate(db); 
  } 
 
} 

数据库的业务封装,其中获取更多数据的是核心代码

package com.example.listviewbatchloading; 
 
/** 
 * 数据库的业务封装类 
 */ 
 
import java.util.arraylist; 
import java.util.list; 
 
import android.content.contentvalues; 
import android.content.context; 
import android.database.cursor; 
import android.database.sqlite.sqlitedatabase; 
 
public class personlist { 
   
  persondb persondb; 
   
  public personlist(context context){ 
    this.persondb = new persondb(context); 
  } 
   
  /** 
   *   获取一定条目的数据 
   * @param startindex 
   *   开始取数据的位置 
   * @param num 
   *   取多少条数据 
   */ 
  public list<person> getmoredatas(int startindex,int num){ 
     
    list<person> list = new arraylist<person>(); 
    sqlitedatabase db = persondb.getwritabledatabase(); 
    cursor cursor = db.rawquery("select name,number from people order by _id desc limit ?,?", 
        new string[]{startindex + "",num + ""}); 
     
    while(cursor.movetonext()){ 
      person person = new person(); 
      person.setname(cursor.getstring(cursor.getcolumnindex("name"))); 
      person.setnumber(cursor.getstring(cursor.getcolumnindex("number"))); 
      list.add(person); 
    } 
     
    cursor.close(); 
    db.close(); 
     
     
    return list; 
     
  } 
   
  /** 
   *   添加数据库条目 
   * @param name 
   * @param number 
   */ 
  public void add(string name,string number){ 
     
    sqlitedatabase db = persondb.getwritabledatabase(); 
     
    contentvalues cv = new contentvalues(); 
    cv.put("name", name); 
    cv.put("number", number); 
     
    db.insert("people", null, cv); 
    db.close(); 
  } 
   
} 

功能实现

package com.example.listviewbatchloading; 
 
import java.util.arraylist; 
import java.util.list; 
 
import android.app.activity; 
import android.os.bundle; 
import android.os.handler; 
import android.os.message; 
import android.view.view; 
import android.view.viewgroup; 
import android.widget.abslistview; 
import android.widget.abslistview.onscrolllistener; 
import android.widget.baseadapter; 
import android.widget.listview; 
import android.widget.textview; 
import android.widget.toast; 
 
public class mainactivity extends activity { 
   
  private listview lv ; 
  private list<person> datas = new arraylist<person>(); 
  private static int perpage = 10; //每页加载数目 
  private static final int finish = 0;//数据加载完成 
  private list<person> moredatas;//每次加载的数据 
  private myadapter adapter; 
  private personlist personlist ; 
   
  //若数据较多,耗时较长,数据加载完成时,发送finish至handler,并通知listview更新数据 
  private handler handler = new handler(){ 
    public void handlemessage(android.os.message msg) { 
       
      switch (msg.what) { 
      case finish: 
        if (moredatas.size() != 0) { 
          system.out.println(moredatas.tostring()); 
          adapter.notifydatasetchanged(); 
        }else { 
          toast.maketext(mainactivity.this, "没有更多数据", toast.length_short).show(); 
        } 
        break; 
 
      default: 
        break; 
      } 
       
       
    }; 
  }; 
   
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
    initview(); 
    initdata(); 
    initevent(); 
  } 
  /** 
   *   为listview添加滚动监听事件,但滚动至最后一行时,加载更多数据 
   */ 
  private void initevent() { 
    lv.setonscrolllistener(new onscrolllistener() { 
       
      @override 
      public void onscrollstatechanged(abslistview view, int scrollstate) { 
        // todo auto-generated method stub 
        if (scrollstate == onscrolllistener.scroll_state_idle) { 
          int lastvisibleposition = lv.getlastvisibleposition(); 
          if (lastvisibleposition == datas.size() - 1) { 
            initdata(); 
            system.out.println("加载更多数据"); 
          } 
        } 
      } 
       
      @override 
      public void onscroll(abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) { 
        // todo auto-generated method stub 
         
      } 
    }); 
  } 
 
  private void initview() { 
     
    setcontentview(r.layout.activity_main); 
     
    lv = (listview) findviewbyid(r.id.lv); 
    personlist = new personlist(getapplicationcontext()); 
    adapter = new myadapter(); 
    lv.setadapter(adapter); 
  } 
   
  /** 
   * 在子线程中加载数据,避免主线程阻塞 
   */ 
  private void initdata() { 
    new thread() { 
 
      public void run() { 
 
        // 加载更多数据 
        moredatas = personlist.getmoredatas(datas.size(), perpage); 
 
        datas.addall(moredatas);// 把一个容器的所有数据加进来 
        // 取数据完成,发消息通知取数据完成 
        handler.obtainmessage(finish).sendtotarget(); 
 
      }; 
    }.start(); 
  } 
   
  private class itemview{ 
    private textview tv_name; 
    private textview tv_num; 
  } 
   
  /** 
   *   listview的适配器 
   * @author lian 
   * 
   */ 
  private class myadapter extends baseadapter{ 
 
    @override 
    public int getcount() { 
      // todo auto-generated method stub 
      return datas.size(); 
    } 
 
    @override 
    public object getitem(int position) { 
      // todo auto-generated method stub 
      return null; 
    } 
 
    @override 
    public long getitemid(int position) { 
      // todo auto-generated method stub 
      return 0; 
    } 
 
    @override 
    public view getview(int position, view convertview, viewgroup parent) { 
       
      itemview itemview = null; 
      if (convertview == null) { 
        itemview = new itemview(); 
        convertview = view.inflate(getapplicationcontext(), r.layout.item_lv, null); 
        itemview.tv_name = (textview) convertview.findviewbyid(r.id.tv_name); 
        itemview.tv_num = (textview) convertview.findviewbyid(r.id.tv_num); 
        convertview.settag(itemview); 
      }else { 
        itemview = (itemview) convertview.gettag(); 
      } 
       
      person person = datas.get(position); 
      itemview.tv_name.settext(person.getname()); 
      itemview.tv_num.settext(person.getnumber()); 
       
      return convertview; 
    } 
     
  } 
   
} 

其他person的javabean,以及布局文件,不在赘述

以上就是本文的全部内容,希望对大家的学习有所帮助。