Android之ListView分页数据加载

分享到:

1、效果如下:



实例如下:  上图的添加数据按钮可以换成一个进度条  因为没有数据所以我加了一个按钮添加到数据库用于测试;一般在服务器拉去数据需要一定的时间,所以可以弄个进度条来提示用户:

点击加载按钮的时候,向数据库读取一次数据,把读取的数据追加到原来的数据集中;然后显示出来


 

package com.exampleandroid.xiong.listviewpages;

public class News {
	private String title;
	private int id;

	/**
	 * 
	 * @return 返回新闻标题
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * 
	 * @param title
	 *            设置新闻标题
	 */
	public void setTtitle(String title) {
		this.title = title;
	}

	/**
	 * 
	 * @return 返回新闻标识
	 */
	public int getId() {
		return id;
	}

	/**
	 * 
	 * @param 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
	 *            需要加载的页数 每页数据5条
	 * @param dbnews
	 *            SQLiteOpenHelper子类
	 * @return 返回新闻加载的数据
	 */
	public List<News> getListNews(int page, DbSqliteNews dbnews) {
		List<News> list = new ArrayList<News>();
		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;
	}

	/**
	 * 插入100条数据用于测试
	 * 
	 * @param dbnews
	 *            SQLiteOpenHelper子类
	 */
	public void insertData(DbSqliteNews dbnews) {
		SQLiteDatabase datas = dbnews.getWritableDatabase();
		datas.beginTransaction();
		try {
			for (int i = 0; i < 100; i++) {
				datas.execSQL("insert into tb_newstile values(?,?)",
						new Object[] { i, "新闻标题" + i });
			}
			datas.setTransactionSuccessful();
		} catch (Exception e) {
			System.out.println("数据插入失败!");
			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);
	}

	// 创建数据库表
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table tb_newstile(news_id integer ,news_title varchar(100))");
	}

	@Override
	public 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;
	// ListView加载的数据
	private List<News> 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;
	}

}

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/newsShow_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/adddata"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="添加数据" />

        <Button
            android:id="@+id/loadmore_bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/loadmore" />
    </LinearLayout>

</RelativeLayout>

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/showimage_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/n_me_l"
        android:contentDescription="@string/imagenews" />

    <TextView
        android:id="@+id/txt_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


 

 


源码下载:http://download.csdn.net/detail/x605940745/6800461

转载请注明出处:http://blog.csdn.net/x605940745

 

昵    称:
验证码:

相关文档: