Android应用中使用ListView来分页显示刷新的内容

  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;

  // ListView加载的数据

  private List shownews;

  private GetNews getnews;

  private DbSqliteNews dbnews;

  // 加载的页数

  private int pagecount = 1;

  @Override

  protected 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();

  //此按钮只为添加测试数据

  adddata.setOnClickListener(new OnClickListener() {

  @Override

  public void onClick(View v) {

  //插入数据

  getnews.insertData(dbnews);

  //获取数据

  shownews = getnews.getListNews(pagecount, dbnews);

  //显示数据

  newShow_list.setAdapter(listadpter);

  }

  });

  loadmore.setOnClickListener(new OnClickListener() {

  @Override

  public void onClick(View v) {

  pagecount++;

  //将数据追加到原集合

  shownews.addAll(getnews.getListNews(pagecount, dbnews));

  //刷新数据

  listadpter.notifyDataSetInvalidated();

  }

  });

  // 第一次加载的数据

  shownews = getnews.getListNews(pagecount, dbnews);

  newShow_list.setAdapter(listadpter);

  }

  class ListAdpaterNews extends BaseAdapter {

  @Override

  public int getCount() {

  return pagecount * 5;

  }

  @Override

  public Object getItem(int position) {

  return position;

  }

  @Override

  public long getItemId(int position) {

  return position;

  }

  @Override

  public 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;

  }

  }

  @Override

  public boolean onCreateOptionsMenu(Menu menu) {

  getMenuInflater().inflate(R.menu.main, menu);

  return true;

  }

  }