Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android操作SQLite數據庫(增、刪、改、查、分頁等)及ListView顯示數據的方法詳解

Android操作SQLite數據庫(增、刪、改、查、分頁等)及ListView顯示數據的方法詳解

編輯:關於Android編程

本文實例講述了Android操作SQLite數據庫(增、刪、改、查、分頁等)及ListView顯示數據的方法。分享給大家供大家參考,具體如下:

由於剛接觸android開發,故此想把學到的基礎知識記錄一下,以備查詢,故此寫的比較啰嗦:

步驟如下:

一、介紹:

此文主要是介紹怎麼使用android自帶的數據庫SQLite,以及把後台的數據用ListView控件顯示

二、新建一個android工程——DBSQLiteOperate

工程目錄:

三、清單列表AndroidManifest.xml的配置為:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.dboperate"
  android:versionCode="1"
  android:versionName="1.0" >
  <uses-sdk android:minSdkVersion="8" />
  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <!--單元測試 加這句-->
    <uses-library android:name="android.test.runner" />
    <activity
      android:name=".DBSQLiteOperateActivity"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
 <instrumentation android:name="android.test.InstrumentationTestRunner"
 android:targetPackage="com.example.dboperate"
 android:label="Test for my app"/>
</manifest>

四、main.xml配置清單:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <LinearLayout android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" >
  <TextView
    android:id="@+id/name"
    android:layout_width="100dip"
    android:layout_height="wrap_content"
    android:text="@string/name"
    android:gravity="center"/>
  <TextView
    android:id="@+id/phone"
    android:layout_width="100dip"
    android:layout_height="wrap_content"
    android:text="@string/phone"
    android:gravity="center"/>
  <TextView
    android:id="@+id/amount"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/amount"
    android:gravity="center"/>
</LinearLayout>
  <ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
  </ListView>
</LinearLayout>

五、item.xml配置清單:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal" >
  <TextView
    android:id="@+id/name"
    android:layout_width="100dip"
    android:layout_height="wrap_content"
    android:text="@string/name"
    android:gravity="center"/>
  <TextView
    android:id="@+id/phone"
    android:layout_width="100dip"
    android:layout_height="wrap_content"
    android:text="@string/phone"
    android:gravity="center"/>
  <TextView
    android:id="@+id/amount"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/amount"
    android:gravity="center"/>
</LinearLayout>

六、string.xml配置清單:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="hello">Hello World, DBSQLiteOperateActivity!</string>
  <string name="app_name">ExampleDBSQLiteOperate8</string>
  <string name="name">姓名</string>
  <string name="phone">電話</string>
  <string name="amount">存款</string>
</resources>

七、DBSQLiteOperateActivity.java Activity類的源碼:

package com.example.dboperate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.adapter.PersonAdapter;
import com.example.domain.Person;
import com.example.service.PersonService;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class DBSQLiteOperateActivity extends Activity {
 ListView listView;
 PersonService personService;
 OnItemClickListener listViewListener;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    listViewListener = new OnItemClickListener(){
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  //得到listView控件
  ListView listView = (ListView)parent;
  //1、如果使用自定義適配器,返回的是Person對象
  //得到該條目數據
//  Person person = (Person)listView.getItemAtPosition(position);
//  //廣播出去
//  Toast.makeText(getApplicationContext(), person.toString(), Toast.LENGTH_LONG).show();
  //2、如果使用showList2()方法中的適配器時,則取得的值是不一樣的,返回的是cursor
//  Cursor cursor = (Cursor)listView.getItemAtPosition(position);
//  int personid = cursor.getInt(cursor.getColumnIndex("_id"));
//  Toast.makeText(getApplicationContext(), personid+"", Toast.LENGTH_LONG).show();
  //3、如果使用showList()方法中的適配器時,則取得的值是不一樣的,返回的是map
  @SuppressWarnings("unchecked")
  Map<String,Object> map = (Map)listView.getItemAtPosition(position);
  String name = map.get("name").toString();
  String personid = map.get("personid").toString();
  Toast.makeText(getApplicationContext(), personid +"-"+ name, Toast.LENGTH_LONG).show();
  }
    };
    listView = (ListView) this.findViewById(R.id.listView);
    listView.setOnItemClickListener(listViewListener);
    personService = new PersonService(this);
    showList();
  }
 private void showList() {
 List<Person> persons = personService.getScrollData(0, 50);
 List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
 for(Person person : persons){
  HashMap<String,Object> item = new HashMap<String,Object>();
  item.put("name", person.getName());
  item.put("phone", person.getPhone());
  item.put("amount", person.getAmount());
  item.put("personid", person.getId());
  data.add(item);
 }
 SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.item, new String[]{"name","phone","amount"}, new int[]{R.id.name,R.id.phone,R.id.amount});
   listView.setAdapter(adapter);
 }
 public void showList2(){
 Cursor cursor = personService.getCursorScrollData(0, 50);
 //該適配器要求返回的結果集cursor必須包含_id字段,所以需要對取得結果集進行處理
 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.item,cursor,new String[]{"name","phone","amount"}, new int[]{R.id.name,R.id.phone,R.id.amount} );
 listView.setAdapter(adapter);
 }
 /**
 * 自定義適配器
 */
 public void showList3(){
 List<Person> persons = personService.getScrollData(0, 50);
 /**
  * 第一個參數:上下文context,第二個參數:要顯示的數據,第三個參數:綁定的條目界面
  */
 PersonAdapter adapter = new PersonAdapter(this, persons, R.layout.item);
 listView.setAdapter(adapter);
 }
}

八、person.java 實體類 源碼:

package com.example.domain;
public class Person {
 private Integer id;
 private String name;
 private String phone;
 private Integer 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 getPhone() {
 return phone;
 }
 public void setPhone(String phone) {
 this.phone = phone;
 }
 public Person(String name, String phone) {
 this.name = name;
 this.phone = phone;
 }
 public Person(Integer id, String name, String phone,Integer amount) {
 super();
 this.id = id;
 this.name = name;
 this.phone = phone;
 this.amount = amount;
 }
 public Person() {
 super();
 }
 public Integer getAmount() {
 return amount;
 }
 public void setAmount(Integer amount) {
 this.amount = amount;
 }
 @Override
 public String toString() {
 return "Person [id=" + id + ", name=" + name + ", phone=" + phone
  + ", amount=" + amount + "]";
 }
}

九、DBOperateHelper.java 業務類源碼:

package com.example.service;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBOperateHelper extends SQLiteOpenHelper {
 public DBOperateHelper(Context context) {//默認創建的數據庫文件保存在<包名>/database/
 //第一個參數是上下文,第二個參數是數據庫名稱,第三個是游標工廠為null時使用 數據庫默認的游標工廠,第四個是數據庫版本號但是不能為0,一般大於0
 super(context, "smallpig", null, 4);
 }
 /**
 * 數據庫每一次被創建時被調用
 */
 @Override
 public void onCreate(SQLiteDatabase sqldb) {
 sqldb.execSQL("create table person(personid integer primary key autoincrement,name varchar(20),phone varchar(12) null)");
 }
  /**
   * 每一次數據庫版本號發生變動時觸發此方法
   * 比如如果想往數據庫中再插入一些表、字段或者其他信息時通過修改數據庫版本號來觸發此方法
   */
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 //db.execSQL("alter table person add phone varchar(12) null");\
 db.execSQL("alter table person add amount Integer null");
 }
}

十、PersonService.java 業務類源碼:

package com.example.service;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.domain.Person;
public class PersonService {
 private DBOperateHelper dbOperateHelper;
 public PersonService(Context context) {
 this.dbOperateHelper = new DBOperateHelper(context);
 }
  /**
   * 保存記錄
   * @param person
   */
 public void save(Person person){
 //得到數據庫實例,裡面封裝了數據庫操作方法
 SQLiteDatabase sqldb = dbOperateHelper.getWritableDatabase();
   //sqldb.execSQL("insert into person(name,phone) values('"+person.getName()+"','"+person.getPhone()+"')");
 //利用占位符可以避免注入,但是注意數組參與與占位符對應的字段要一一對應
 sqldb.execSQL("insert into person(name,phone,amount) values(?,?,?)",new Object[]{person.getName(),person.getPhone(),person.getAmount()});
   //關閉數據庫
 sqldb.close();
 }
 /**
 * 刪除記錄
 * @param id
 */
 public void delete(Integer id){
 SQLiteDatabase sqldb = dbOperateHelper.getWritableDatabase();
 sqldb.execSQL("delete from person where personid=?",new Object[]{id});
 sqldb.close();
 }
 /**
 * 更新記錄
 * @param person
 */
 public void update(Person person){
 SQLiteDatabase sqldb = dbOperateHelper.getWritableDatabase();
 sqldb.execSQL("update person set name=?,phone=?,amount=? where personid=?",new Object[]{person.getName(),person.getPhone(),person.getAmount(),person.getId()});
 sqldb.close();
 }
 /**
 * 通過ID查詢記錄
 * @param id
 * @return
 */
 public Person find(Integer id){
 /**
  * getWritableDatabase 與 getReadableDatabase 的區別:
  * getReadableDatabase會先返回getWritableDatabase(可寫),如果調用getWritableDatabase失敗
  * 則才會調用getReadableDatabase後續方法,使數據庫只讀
  * 當寫入的數據超過數據庫大小則調用getWritableDatabase會失敗
  * 所以只讀時則可以使用此方法,其它情況(只要不是超過數據庫大小)也可以使用此方法
  */
 SQLiteDatabase sqldb = dbOperateHelper.getReadableDatabase();
 Cursor cursor = sqldb.rawQuery("select * from person where personid=?", new String[]{String.valueOf(id)});
 int personid;
 String name;
 String phone;
 int amount;
 Person person = null;
 if(cursor.moveToFirst()){
  personid = cursor.getInt(cursor.getColumnIndex("personid"));
  name = cursor.getString(cursor.getColumnIndex("name"));
  phone = cursor.getString(cursor.getColumnIndex("phone"));
  amount = cursor.getInt(cursor.getColumnIndex("amount"));
  person = new Person(personid,name,phone,amount);
 }
 cursor.close();
 return person;
 }
 /**
 * 返回指定長度記錄,limit 3,5,適用於分頁
 * @param offset 起始
 * @param maxResult 長度
 * @return
 */
 public List<Person> getScrollData(int offset,int maxResult){
 SQLiteDatabase sqldb = dbOperateHelper.getReadableDatabase();
 Cursor cursor = sqldb.rawQuery("select * from person order by personid asc limit ?,?", new String[]{String.valueOf(offset),String.valueOf(maxResult)});
 int personid;
 String name;
 String phone;
 int amount;
 Person person = null;
 List<Person> persons = new ArrayList<Person>();
 while(cursor.moveToNext()){
  personid = cursor.getInt(cursor.getColumnIndex("personid"));
  name = cursor.getString(cursor.getColumnIndex("name"));
  phone = cursor.getString(cursor.getColumnIndex("phone"));
  amount = cursor.getInt(cursor.getColumnIndex("amount"));
  person = new Person(personid,name,phone,amount);
  persons.add(person);
 }
 cursor.close();
 return persons;
 }
 /**
 * 返回cursor
 * @param offset 起始
 * @param maxResult 長度
 * @return
 */
 public Cursor getCursorScrollData(int offset,int maxResult){
 SQLiteDatabase sqldb = dbOperateHelper.getReadableDatabase();
 Cursor cursor = sqldb.rawQuery("select personid as _id,name,phone,amount from person order by personid asc limit ?,?", new String[]{String.valueOf(offset),String.valueOf(maxResult)});
 return cursor;
 }
 /**
 * 返回總記錄數
 * @return
 */
 public long getCount(){
 SQLiteDatabase sqldb = dbOperateHelper.getReadableDatabase();
 Cursor cursor = sqldb.rawQuery("select count(*) from person", null);
 //該查詢語句值返回一條語句
 cursor.moveToFirst();
 long result = cursor.getLong(0);
 cursor.close();
 return result;
 }
 public void payment(){
 SQLiteDatabase sqldb = dbOperateHelper.getWritableDatabase();
 sqldb.beginTransaction();//開啟事務
 try{
  sqldb.execSQL("update person set amount = amount -10 where personid=1");
  sqldb.execSQL("update person set amount = amount + 10 where personid=2");
  sqldb.setTransactionSuccessful();//設置事務標志位true
 } finally {
  //結束事務:有兩種情況:commit\rollback,事務提交或者回滾是由事務的標識決定的
  //事務為ture則提交,事務為flase則回滾,默認為false
  sqldb.endTransaction();
 }
 }
}

十一、OtherPersonService.java 業務類源碼:

package com.example.service;
import java.util.ArrayList;
import java.util.List;
import com.example.domain.Person;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class OtherPersonService {
 private DBOperateHelper dbOperateHelper;
 public OtherPersonService(Context context) {
 this.dbOperateHelper = new DBOperateHelper(context);
 }
  /**
   * 保存記錄
   * @param person
   */
 public void save(Person person){
 //得到數據庫實例,裡面封裝了數據庫操作方法
 SQLiteDatabase sqldb = dbOperateHelper.getWritableDatabase();
   //sqldb.execSQL("insert into person(name,phone) values('"+person.getName()+"','"+person.getPhone()+"')");
 //利用占位符可以避免注入,但是注意數組參與與占位符對應的字段要一一對應
 //sqldb.execSQL("insert into person(name,phone) values(?,?)",new Object[]{person.getName(),person.getPhone()});
 ContentValues values = new ContentValues();
 values.put("name", person.getName());
 values.put("phone", person.getPhone());
 values.put("amount", person.getAmount());
 //第一個參數是表名,第三個為字段值集合,第二個參數是空值字段,當第三個字段值集合為空時,系統會自動插入一條第二個參數為空的sql語句
 //否則當第三個參數為空時,如果第二個參數也為空,那麼插入表就會找不到插入的字段信息,會報錯
 sqldb.insert("person", "name", values );
 //關閉數據庫
 sqldb.close();
 }
 /**
 * 刪除記錄
 * @param id
 */
 public void delete(Integer id){
 SQLiteDatabase sqldb = dbOperateHelper.getWritableDatabase();
 //sqldb.execSQL("delete from person where personid=?",new Object[]{id});
 //第一個參數是表名,第二個是where後面的條件用占位符表示,第三個對應占位符為參數值
 sqldb.delete("person", "personid=?", new String[]{Integer.toString(id)});
 sqldb.close();
 }
 /**
 * 更新記錄
 * @param person
 */
 public void update(Person person){
 SQLiteDatabase sqldb = dbOperateHelper.getWritableDatabase();
 //sqldb.execSQL("update person set name=?,phone=? where personid=?",new Object[]{person.getName(),person.getPhone(),person.getId()});
 //第一個參數為表名,第二個是一個更新值集合,采用鍵值對的形式,每個更新的字段對應更新值
 //第三個參數是where後面條件字段用占位符標識,第四個參數是對應where占位符的值
 ContentValues values = new ContentValues();
 values.put("name", person.getName());
 values.put("phone", person.getPhone());
 values.put("amount", person.getAmount());
 sqldb.update("person", values , "personid=?", new String[]{person.getId().toString()});
 sqldb.close();
 }
 /**
 * 通過ID查詢記錄
 * @param id
 * @return
 */
 public Person find(Integer id){
 /**
  * getWritableDatabase 與 getReadableDatabase 的區別:
  * getReadableDatabase會先返回getWritableDatabase(可寫),如果調用getWritableDatabase失敗
  * 則才會調用getReadableDatabase後續方法,使數據庫只讀
  * 當寫入的數據超過數據庫大小則調用getWritableDatabase會失敗
  * 所以只讀時則可以使用此方法,其它情況(只要不是超過數據庫大小)也可以使用此方法
  */
 SQLiteDatabase sqldb = dbOperateHelper.getReadableDatabase();
 //Cursor cursor = sqldb.rawQuery("select * from person where personid=?", new String[]{String.valueOf(id)});
 //第一個參數是表名;第二個參數是查詢顯示的字段,null時默認查詢顯示所有字段;
 //第三個參數是where查詢條件占位符;第四個是占位符對應的值;
 //第五個參數是group by條件;第六個是having條件;第七個是order by條件
 Cursor cursor = sqldb.query("person", null, "personid=?", new String[]{id.toString()}, null, null, null);
 int personid;
 String name;
 String phone;
 int amount;
 Person person = null;
 if(cursor.moveToFirst()){
  personid = cursor.getInt(cursor.getColumnIndex("personid"));
  name = cursor.getString(cursor.getColumnIndex("name"));
  phone = cursor.getString(cursor.getColumnIndex("phone"));
  amount = cursor.getInt(cursor.getColumnIndex("amount"));
  person = new Person(personid,name,phone,amount);
 }
 cursor.close();
 return person;
 }
 /**
 * 返回指定長度記錄,limit 3,5,適用於分頁
 * @param offset 起始
 * @param maxResult 長度
 * @return
 */
 public List<Person> getScrollData(int offset,int maxResult){
 SQLiteDatabase sqldb = dbOperateHelper.getReadableDatabase();
 //Cursor cursor = sqldb.rawQuery("select * from person order by personid asc limit ?,?", new String[]{String.valueOf(offset),String.valueOf(maxResult)});
 //第一個參數是表名;第二個參數是查詢顯示的字段,null時默認查詢顯示所有字段;
 //第三個參數是where查詢條件占位符;第四個是占位符對應的值;
 //第五個參數是group by條件;第六個是having條件;第七個是order by條件
 //第八個參數是limit ?,? 條件
 Cursor cursor = sqldb.query("person", null, null, null, null, null, "personid",offset+","+maxResult);
 int personid;
 String name;
 String phone;
 int amount;
 Person person = null;
 List<Person> persons = new ArrayList<Person>();
 while(cursor.moveToNext()){
  personid = cursor.getInt(cursor.getColumnIndex("personid"));
  name = cursor.getString(cursor.getColumnIndex("name"));
  phone = cursor.getString(cursor.getColumnIndex("phone"));
  amount = cursor.getInt(cursor.getColumnIndex("amount"));
  person = new Person(personid,name,phone,amount);
  persons.add(person);
 }
 cursor.close();
 return persons;
 }
 /**
 * 返回總記錄數
 * @return
 */
 public long getCount(){
 SQLiteDatabase sqldb = dbOperateHelper.getReadableDatabase();
 //Cursor cursor = sqldb.rawQuery("select count(*) from person", null);
 //第一個參數是表名;第二個參數是查詢顯示的字段,null時默認查詢顯示所有字段;
 //第三個參數是where查詢條件占位符;第四個是占位符對應的值;
 //第五個參數是group by條件;第六個是having條件;第七個是order by條件
 Cursor cursor = sqldb.query("person", new String[]{"count(*)"}, null, null, null, null, null);
 //該查詢語句值返回一條語句
 cursor.moveToFirst();
 long result = cursor.getLong(0);
 cursor.close();
 return result;
 }
}

十二、PersonServiceTest.java 單元測試類源碼:

package com.example.test;
import java.util.List;
import com.example.domain.Person;
import com.example.service.DBOperateHelper;
import com.example.service.PersonService;import android.test.AndroidTestCase;
import android.util.Log;
public class PersonServiceTest extends AndroidTestCase {
 public void testCreateDB() throws Exception{
 DBOperateHelper dbHelper = new DBOperateHelper(getContext());
 dbHelper.getWritableDatabase();
 }
 public void testSave() throws Exception{
 PersonService ps = new PersonService(getContext());
 for(int i=1;i<=100;i++){
  Person person = new Person();
  person.setName("我是"+i);
  person.setPhone(String.valueOf(Long.parseLong("18888888800")+i));
  ps.save(person);
  Log.i("PersonService",person.toString());
 }
 }
 public void testDelete() throws Exception{
 PersonService ps = new PersonService(getContext());
 ps.delete(10);
 }
 public void testUpdate() throws Exception{
 PersonService ps = new PersonService(getContext());
 ps.update(new Person(1,"xiaopang","18887654321",0));
 }
 public void testFind() throws Exception{
 PersonService ps = new PersonService(getContext());
 Person person = ps.find(1);
 Log.i("PersonService", person.toString());
 }
 public void testGetScrollData() throws Exception{
 PersonService ps = new PersonService(getContext());
 List<Person> persons = ps.getScrollData(3, 5);
 for(Person person:persons){
  Log.i("PersonService",person.toString());
 }
 }
 public void testGetCount() throws Exception{
 PersonService ps = new PersonService(getContext());
 Long count = ps.getCount();
 Log.i("PersonService",count.toString());
 }
 public void testUpdateAmount() throws Exception{
 PersonService ps = new PersonService(getContext());
 Person person1 = ps.find(1);
 Person person2 = ps.find(2);
 person1.setAmount(100);
 person2.setAmount(100);
 ps.update(person1);
 ps.update(person2);
 }
 public void testPayment() throws Exception{
 PersonService ps = new PersonService(getContext());
 ps.payment();
 }
}

十三、OtherPersonServiceTest 單元測試類源碼:

package com.example.test;
import java.util.List;
import com.example.domain.Person;
import com.example.service.DBOperateHelper;
import com.example.service.OtherPersonService;
import android.test.AndroidTestCase;
import android.util.Log;
public class OtherPersonServiceTest extends AndroidTestCase {
 public void testCreateDB() throws Exception{
 DBOperateHelper dbHelper = new DBOperateHelper(getContext());
 dbHelper.getWritableDatabase();
 }
 public void testSave() throws Exception{
 OtherPersonService ps = new OtherPersonService(getContext());
 for(int i=1;i<=100;i++){
  Person person = new Person();
  person.setName("我是"+i);
  person.setPhone(String.valueOf(Long.parseLong("18888888800")+i));
  ps.save(person);
  Log.i("PersonService",person.toString());
 }
 }
 public void testDelete() throws Exception{
 OtherPersonService ps = new OtherPersonService(getContext());
 ps.delete(10);
 }
 public void testUpdate() throws Exception{
 OtherPersonService ps = new OtherPersonService(getContext());
 ps.update(new Person(1,"xiaopang","18887654321",0));
 }
 public void testFind() throws Exception{
 OtherPersonService ps = new OtherPersonService(getContext());
 Person person = ps.find(1);
 Log.i("PersonService", person.toString());
 }
 public void testGetScrollData() throws Exception{
 OtherPersonService ps = new OtherPersonService(getContext());
 List<Person> persons = ps.getScrollData(3, 5);
 for(Person person:persons){
  Log.i("PersonService",person.toString());
 }
 }
 public void testGetCount() throws Exception{
 OtherPersonService ps = new OtherPersonService(getContext());
 Long count = ps.getCount();
 Log.i("PersonService",count.toString());
 }
}

十四、注意事項以及相關知識點:

1、掌握SQLite數據庫如何創建數據庫、建立表、維護字段等操作
繼承SQLiteOpenHelper類,構造函數調用父類構造函數創建數據庫,利用onCreate創建表,利用onUpgrade更新表字段信息

2、掌握SQLite數據庫如何增、刪、改、查以及分頁
取得SQLiteDatabase的實例,然後調用該實例的方法可以完成上述操作
SQLiteDataBase提供兩種操作上述功能的方式:一是直接調用execSQL書寫sql語句,另一種是通過insert、update、delete、query等方法來傳值來拼接sql,前一種適合熟練掌握sql 語句的

3、對需要數據同步的處理請添加事務處理,熟悉事務的處理方式

4、了解各個方法參數的意義以及傳值

5、掌握ListView顯示後台數據的使用方法
SimpleAdapter、SimpleCursorAdapter以及自定義適配器的使用,以及OnItemClickListener取值時各個適配器返回值的區別以及取值方法

6、多學、多記、多練、多思,加油!

更多關於Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android通信方式總結》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》

希望本文所述對大家Android程序設計有所幫助。

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