|
很久没有写东西了,一来是因为项目紧,没有多少时间,二来是因为最近越来越懒了。。。。 今天说说数据库的分页显示问题,都是些自己在项目中碰到的问题,写在这里,留作以后复习用。。。。
所谓数据库的分页显示,必须先要有一个数据库,先创建一个数据库。我这里用的是继承SQLiteOpenHelper的方法。具体如下:- *package com.android.database;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.provider.BaseColumns;
- /**
- *
- * @author shangzhenxiang
- * 创建数据库,继承SQLiteOpenHelper。
- *
- */
- public class DBHelper extends SQLiteOpenHelper {
- private static final String DATABASE_NAME = "database.db";
- private static final int DATABASE_VERSION = 1;
- private static final String TABLE_NAME = "database";
- public static final String FIELD_TITLE = "title";
- public DBHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- /**
- * 创建表,写一个创建表的sql语句,然后调用db.execSQL(sql);之后向表中添加几条数据。
- */
- @Override
- public void onCreate(SQLiteDatabase db) {
- String sql="Create table "+TABLE_NAME+"("+BaseColumns._ID+" integer primary key autoincrement,"
- + FIELD_TITLE + " text );";
- db.execSQL(sql);
- //初始化数据库
- initDatabase(db);
- }
- //向数据库的表中插入一些数据。
- private void initDatabase(SQLiteDatabase db) {
- ContentValues cv = new ContentValues();
- cv.put("title", "cctv1 news");
- db.insert(TABLE_NAME, null, cv);
- cv.clear();
- cv.put("title", "cctv2 news");
- db.insert(TABLE_NAME, null, cv);
- cv.clear();
- cv.put("title", "cctv3 news");
- db.insert(TABLE_NAME, null, cv);
- cv.clear();
- cv.put("title", "cctv4 news");
- db.insert(TABLE_NAME, null, cv);
- cv.clear();
- cv.put("title", "cctv5 news");
- db.insert(TABLE_NAME, null, cv);
- cv.clear();
- cv.put("title", "cctv6 news");
- db.insert(TABLE_NAME, null, cv);
- cv.clear();
- cv.put("title", "cctv7 news");
- db.insert(TABLE_NAME, null, cv);
- cv.clear();
- cv.put("title", "cctv8 news");
- db.insert(TABLE_NAME, null, cv);
- cv.clear();
- cv.put("title", "cctv9 news");
- db.insert(TABLE_NAME, null, cv);
- cv.clear();
- cv.put("title", "cctv10 news");
- db.insert(TABLE_NAME, null, cv);
- cv.clear();
- cv.put("news_title", "guangshui tv");
- db.insert(TABLE_NAME, null, cv);
- }
- //这里是用于更新数据库的。
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- String sql=" DROP TABLE IF EXISTS "+TABLE_NAME;
- db.execSQL(sql);
- onCreate(db);
- }
- }
复制代码 代码注释的比较清楚,数据库很简单,只有一个id 和 title键值,id是自动增长的,是系统自动的,在创建数据库的时候,我在里面加了9跳数据,用作测试用。 数据库写完了,那好,我们要测试一下,到底生成了数据库文件没有呢?
这时我们要写一个测试程序,来生成数据库文件,这需要在 manifest文件中做一些处理,如下所示:- package com.android.database;
- import android.test.AndroidTestCase;
- /**
- *
- * @author shangzhenxiang
- *
- */
- public class DBTest extends AndroidTestCase {
- public void testCreateRecordedDB() throws Exception {
- DBHelper recordedDbHelper = new DBHelper(this.getContext());
- /**
- * 当我们调用recordedDbHelper的getWritableDatabase()或getReadableDatabase()的时候,
- * 都会导致数据库的生成
- */
- recordedDbHelper.getWritableDatabase();
- // recordedDbHelper.getReadableDatabase();
- }
- }
复制代码 生成的数据库文件在哪里呢? 在data文件夹下的data文件夹中找到刚刚在manifest中的targetPackage:

目标包中有个database的文件夹,里面就是生成的数据库文件,如下图:

数据库完成了,我们需要写一个单独的类用来显示页数和每一页上的个数,如图所示:- package com.android.domain;
- public class Model {
- /**
- * @author shangzhenxiang
- * index 用于显示页数
- * View_Count 用于每一页上显示的个数
- */
- private int index;
- private int View_Count;
- public Model(int index, int View_Count) {
- this.index = index;
- this.View_Count = View_Count;
- }
- public int getIndex() {
- return index;
- }
- public void setIndex(int index) {
- this.index = index;
- }
- public int getView_Count() {
- return View_Count;
- }
- public void setView_Count(int view_Count) {
- View_Count = view_Count;
- }
- }
复制代码 我们还需要一个服务来操作数据库,就是我们常说的业务逻辑层。 我们需要对数据库做什么操作,都可以写到这个类中:- package com.android.service;
- import java.util.ArrayList;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import com.android.database.DBHelper;
- /**
- *
- * @author shangzhenxiang
- *
- */
- public class DatabaseService {
- private Context mContext;
- private DBHelper dbHelper;
- public DatabaseService(Context context) {
- // TODO Auto-generated constructor stub
- mContext = context;
- dbHelper = new DBHelper(mContext);
- }
- //添加
- public void insert(String title) {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- String sql = "insert into database(title) values(?)";
- db.execSQL(sql, new String[]{title});
- }
- //删除
- public void delete(String title) {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- String sql = "delete from database where title = ?";
- db.execSQL(sql, new String[]{title});
- }
- //查找
- public ArrayList<String> find(int id) {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- String sql = "select * from database where _id = ? ";
- Cursor c = db.rawQuery(sql, new String[]{String.valueOf(id)});
- ArrayList<String> titles = new ArrayList<String>();
- if(c.moveToNext()) {
- String title =c.getString(c.getColumnIndexOrThrow(DBHelper.FIELD_TITLE));
- titles.add(title);
- return titles;
- }
- //不用忘记关闭Cursor。
- c.close();
- return null;
- }
- //更新
- public void upDate(int id, String title) {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- String sql = "update database set title =? where _id = ?";
- db.execSQL(sql, new String[]{String.valueOf(title), String.valueOf(id)});
- }
- //查询记录的总数
- public long getCount() {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- String sql = "select count(*) from database";
- Cursor c = db.rawQuery(sql, null);
- c.moveToFirst();
- long length = c.getLong(0);
- c.close();
- return length;
- }
- /**
- * 拿到所有的记录条数
- * @param firstResult 从第几条数据开始查询。
- * @param maxResult 每页显示多少条记录。
- * @return 当前页的记录
- */
- public Cursor getAllItems(int firstResult, int maxResult) {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- String sql = "select * from database limit ?,?";
- Cursor mCursor = db.rawQuery(sql, new String[]{String.valueOf(firstResult), String.valueOf(maxResult)});
- return mCursor;
- }
- }
复制代码 写一个布局。里面有一个ListView和2个Button:- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <ListView
- android:id="@+id/testList"
- android:layout_width="match_parent"
- android:layout_height="0dip"
- android:layout_weight="3"/>
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="0dip"
- android:layout_weight="1">
- <Button
- android:id="@+id/leftButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/previous"/>
- <Button
- android:id="@+id/rightButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/next"/>
- </LinearLayout>
- </LinearLayout>
复制代码 我们需要对这个ListView设置一个Adapter,如下:- package com.android.sqlite;
- import android.content.Context;
- import android.database.Cursor;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.CursorAdapter;
- import android.widget.TextView;
- import com.android.domain.Model;
- /**
- *
- * @author shangzhenxiang
- *
- */
- public class TestListAdapter extends CursorAdapter {
- private Context mContext;
- private Cursor mCursor;
- private Model mModel;
- private LayoutInflater mInflater;
- public TestListAdapter(Context context, Cursor c, Model model) {
- super(context, c);
- System.out.println("c = " + c);
- this.mContext = context;
- this.mCursor = c;
- this.mModel = model;
- mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- @Override
- public View newView(Context context, Cursor cursor, ViewGroup parent) {
- return mInflater.inflate(R.layout.listitem, parent, false);
- }
- @Override
- public void bindView(View view, Context context, Cursor cursor) {
- ViewHolder holder = null;
- Object tag = view.getTag();
- if(tag instanceof ViewHolder) {
- holder = (ViewHolder) view.getTag();
- }
- if(holder == null) {
- holder = new ViewHolder();
- view.setTag(holder);
- holder.title = (TextView) view.findViewById(R.id.listitemtitle);
- }
- //将从数据库中查询到的title设为ListView的Item项。
- holder.title.setText(cursor.getString(cursor.getColumnIndexOrThrow("title")));
- }
- static class ViewHolder {
- TextView title;
- }
- }
复制代码 传进去一个Model的参数和一个Cursor。Model用于显示每页的个数和第几页,Cursor用于传递数据: 在Activity中我们对Button做监听:
向左移的时候就将Model中的index减一,向右就加一,同时改变cursor中传进去的Model的值,刷新Adapter,刷新界面,
同时检查Button的可用性:代码中的注释写的比较清楚,记性不太好,留作以后复习用。代码:
TestSqlite
源文摘自:http://www.cnblogs.com/shang53880/archive/2011/03/11/1981749.html
|
|