(1)Contact.java
package com.example.l0831_sqlite_all; /** * Contact类,封装数据 * @author asus */ public class Contact { private Integer _id; private String name; private String phone; public Contact() { super(); } public Contact(String name, String phone) { this.name = name; this.phone = phone; } public Contact(int id,String name, String phone) { this._id=id; this.name = name; this.phone = phone; } public Integer get_id() { return _id; } public void set_id(Integer id) { _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; } @Override public String toString() { return "Contants [id=" + _id + ", name=" + name + ", phone=" + phone + "]"; } }
(2)MyOpenHelper.java
package com.example.l0831_sqlite_all; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MyOpenHelper extends SQLiteOpenHelper{ private static final String name = "contants"; // 数据库名称 private static final int version = 1; // 数据库版本 public MyOpenHelper(Context context) { /** * 第三个参数: * CursorFactory指定在执行查询时获得一个游标实例的工厂类。 * 设置为null,则使用系统默认的工厂类。 */ super(context, name, null, version); } @Override public void onCreate(SQLiteDatabase db) { // 创建contacts表,SQL表达式时提供的字段类型和长度仅为提高代码的可读性。 db.execSQL("CREATE TABLE IF NOT EXISTS contacts(" + "_id integer primary key autoincrement," + "name varchar(20)," + "phone varchar(50))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // 仅演示用,所以先删除表然后再创建。 db.execSQL("DROP TABLE IF EXISTS contacts"); this.onCreate(db); } }
(3)ContactService.java
package com.example.l0831_sqlite_all; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; /** * 这个类的功能是把操作数据库的方法封装在里面 * @author asus */ public class ContactsService { private MyOpenHelper openHelper; // 构造方法,仅需传入SQLiteOpenHelper的实例即可 public ContactsService(Context context) { this.openHelper = new MyOpenHelper(context); } /** * 保存 * * @param contact */ public void save(Contact contact) { //?表示传入的参数 String sql = "INSERT INTO contacts (name, phone) VALUES (?, ?)"; Object[] bindArgs = { contact.getName(), contact.getPhone() }; //获取数据库可写权限并执行插入的SQL语句 this.openHelper.getWritableDatabase().execSQL(sql, bindArgs); } /** * 查找 * @param id * @return */ public Contact find(Integer id) { String sql = "SELECT _id,name, phone FROM contacts WHERE _id=?"; String[] selectionArgs = { id + "" }; //查询出数据,放到Cursor对象中 //获取数据库可写权限,并执行查询语句(rawQuery) Cursor cursor = this.openHelper.getReadableDatabase().rawQuery(sql,selectionArgs); if (cursor.moveToFirst())//如果cursor不为空 return new Contact(cursor.getInt(0), cursor.getString(1),cursor.getString(2)); return null; } /** * 更新 * @param contact */ public void update(Contact contact) { String sql = "UPDATE contacts SET name=?, phone=? WHERE _id=?"; Object[] bindArgs = { contact.getName(), contact.getPhone(),contact.get_id() }; this.openHelper.getWritableDatabase().execSQL(sql, bindArgs); } /** * 删除 * @param id */ public void delete(Integer id) { String sql = "DELETE FROM contacts WHERE _id=?"; Object[] bindArgs = { id }; this.openHelper.getReadableDatabase().execSQL(sql, bindArgs); } /** * 获取记录数量 * @return */ public long getCount() { String sql = "SELECT count(*) FROM contacts"; Cursor cursor = this.openHelper.getReadableDatabase().rawQuery(sql,null); cursor.moveToFirst(); return cursor.getInt(0); } /** * 获取分页数据 * @param startIndex * @param maxCount * @return */ public List<Contact> getScrollData(long startIndex, long maxCount) { String sql = "SELECT _id,name,phone FROM contacts LIMIT ?,?"; String[] selectionArgs = { String.valueOf(startIndex),String.valueOf(maxCount) }; Cursor cursor = this.openHelper.getReadableDatabase().rawQuery(sql,selectionArgs); List<Contact> contacts = new ArrayList<Contact>(); while (cursor.moveToNext()) { Contact contact = new Contact(cursor.getInt(0),cursor.getString(1), cursor.getString(2)); contacts.add(contact); } return contacts; } /** * 获取分页数据,提供给SimpleCursorAdapter使用。 * @param startIndex * @param maxCount * @return */ public Cursor getScrollDataCursor(long startIndex, long maxCount) { String sql = "SELECT _id,name,phone FROM contacts LIMIT ?,?"; String[] selectionArgs = { String.valueOf(startIndex),String.valueOf(maxCount) }; Cursor cursor = this.openHelper.getReadableDatabase().rawQuery(sql,selectionArgs); return cursor; } }
(4)MainActivity.java
package com.example.l0831_sqlite_all; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建ContactsService的对象(操作数据库) ContactsService contactsService = new ContactsService(this); Contact contact=new Contact(1001,"塔诺","18867899086"); contactsService.save(contact); contactsService.find(1001); // 获取分页数据 ,提供给SimpleCursorAdapter使用 Cursor cursor = contactsService.getScrollDataCursor(0, 3); // 获取ListView ListView lv = (ListView) this.findViewById(R.id.listView); // 创建SimpleCursorAdapter SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, R.layout.contact_cell, //ListView引用的资源文件 cursor, //获取数据库数据的游标对象 new String[] { "_id", "name","phone" },//封装表中的列名 new int[] { R.id.tv_id, R.id.tv_name, R.id.tv_phone }); //封装ListView资源文件中的控件的id // 设置ListView适配器 lv.setAdapter(adapter); // 为ListView添加事件 lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Cursor cursor = (Cursor) parent.getItemAtPosition(position); Toast.makeText(MainActivity.this, cursor.getString(1), Toast.LENGTH_SHORT).show(); } }); } }
(5)运行结果: