Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android 分頁加載ListView

Android 分頁加載ListView

編輯:高級開發

 分頁加載代碼 收藏代碼

  package com.xmz.listvIEw;

  import android.app.Activity;

  import android.database.Cursor;

  import android.os.Bundle;

  import android.os.Handler;

  import android.os.Message;

  import android.view.VIEw;

  import android.widget.AdapterVIEw;

  import android.widget.ListVIEw;

  import android.widget.ProgressBar;

  import android.widget.TextVIEw;

  import android.widget.AdapterVIEw.OnItemSelectedListener;

  public class MainActivity extends Activity {

  private ListView listvIEw;

  private ProgressBar loadBar;

  private TextVIEw loadText;

  private ShopAdapter adapter;

  private int start=0;//開始數量

  private int limit=3;//每頁數量

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentVIEw(R.layout.main);

  listview=(ListView)findViewById(R.id.main_ListVIEw);

  loadBar=(ProgressBar)findVIEwById(R.id.main_loadBar);

  loadText=(TextView)findVIEwById(R.id.main_loadText);

  adapter=new ShopAdapter(this);

  listvIEw.setAdapter(adapter);

  listvIEw.setOnItemSelectedListener(new OnItemSelectedListener(){

  @Override

  public void onItemSelected(AdapterView arg0, VIEw arg1,

  int count, long arg3) {

  if(adapter.getCount()==count+1){

  loadText.setText("下載中...");

  loadBar.setVisibility(VIEw.VISIBLE);

  new Thread(mRunnable).start();

  }

  }

  @Override

  public void onNothingSelected(AdapterVIEw arg0) {

  }

  });

  new Thread(mRunnable).start();

  }

  /**

  * 商店列表線程

  */

  private Runnable mRunnable=new Runnable(){

  接上頁

  public void run(){

  try {

  Thread.sleep(1500);

  mHandler.sendMessage(mHandler.obtainMessage());

  } catch (InterruptedException e) {

  //System.out.println("Error-"+e.getMessage());

  }

  }

  };

  Handler mHandler=new Handler(){

  public void handleMessage(Message msg){

  super.handleMessage(msg);

  loadHotShop(limit);

  }

  };

  /**

  * 加載店鋪

  * @return

  */

  public void loadHotShop(int limit){

  DBLocalService db=new DBLocalService(this);

  Cursor cursor=db.querySQL("select * from "+db.TABLE_SHOP+" limit "+start+","+limit,null);

  if(cursor.getCount()>0) this.start+=limit;

  while(cursor.moveToNext()){

  Shop s=new Shop();

  s.setId(cursor.getString(cursor.getColumnIndex("id")));

  s.setTitle(cursor.getString(cursor.getColumnIndex("title")));

  adapter.addShop(s);

  }

  loadText.setText("");

  loadBar.setVisibility(VIEw.GONE);

  }

  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved