//目錄結構
//編寫Bean類
- package sn.len.transandlistview.domain;
-
- public class PersonBean
- {
- private Integer id;
- private String name;
- private String amount;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAmount() {
- return amount;
- }
- public void setAmount(String amount) {
- this.amount = amount;
- }
- @Override
- public String toString() {
- return "PersonBean [id=" + id + ", name=" + name + ", amount=" + amount
- + "]";
- }
- public PersonBean(Integer id, String name)
- {
- this.id = id;
- this.name = name;
- }
- public PersonBean() {
-
- }
-
-
-
- }
//編寫DB類
DBOpenHelper.java 創建表,以及數據庫
- package sn.len.transandlistview;
-
- import Android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
-
- public class DBOpenHelper extends SQLiteOpenHelper
- {
- private static final String TAG="testinfo";
-
- private static final String DBNAME="transdb.db";
- private static final int DBVERSION=1;
- public DBOpenHelper(Context context)
- {
- super(context,DBNAME,null,DBVERSION);
- Log.i(TAG,"come in there");
- }
-
- @Override
- public void onCreate(SQLiteDatabase db)
- {
- db.execSQL("create table PersonBean(id integer primary key autoincrement,name varchar(20),amount varchar(20))");
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
- {
-
- }
-
- }
//編寫業務類,Service
- package sn.len.transandlistview.service;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import sn.len.transandlistview.DBOpenHelper;
- import sn.len.transandlistview.domain.PersonBean;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.util.Log;
-
- public class TransListviewService
- {
- private DBOpenHelper dbopen;
- public TransListviewService(Context context)
- {
- this.dbopen=new DBOpenHelper(context);
- this.dbopen.getWritableDatabase();
- }
- //添加數據
- public void save(PersonBean person)
- {
- SQLiteDatabase sqld=dbopen.getWritableDatabase();
- Log.i("name", person.getName());
- Log.i("amount", person.getAmount());
- sqld.execSQL("insert into PersonBean(name,amount) values(?,?)",new Object[]{person.getName(),person.getAmount()});
- }
- //事務處理
- public void trans()
- {
- SQLiteDatabase sqld=dbopen.getWritableDatabase();
- sqld.beginTransaction();
- sqld.execSQL("update PersonBean set name=? where id=?",new Object[]{"admin",1});
- sqld.execSQL("update PersonBean set name=? where id=?",new Object[]{"administrator",2});
- sqld.setTransactionSuccessful();
- sqld.endTransaction();
- }
- //查詢分頁
- public List<PersonBean> selectGroup(Integer offset,Integer maxRow)
- {
- SQLiteDatabase sqld=dbopen.getWritableDatabase();
- List<PersonBean> listPb=new ArrayList<PersonBean>();
- Cursor cursor=sqld.rawQuery("select * from PersonBean limit ?,?",new String[]{offset.toString(),maxRow.toString()});
- while(cursor.moveToNext())
- {
- PersonBean pb=new PersonBean();
- pb.setId(cursor.getInt(cursor.getColumnIndex("id")));
- pb.setName(cursor.getString(cursor.getColumnIndex("name")));
- pb.setAmount(cursor.getString(cursor.getColumnIndex("amount")));
- listPb.add(pb);
- }
- return listPb;
- }
- //通過返回郵標來得到顯示的數據
- public Cursor getCursor(Integer offset,Integer maxRow)
- {
- SQLiteDatabase sqld=dbopen.getWritableDatabase();
- //如果使用郵標那麼ID必需設置尖_id才行。
- Cursor cursor=sqld.rawQuery("select id as _id,name,amount from PersonBean limit ?,?",new String[]{offset.toString(),maxRow.toString()});
- return cursor;
- }
- }