Activity文件代码 package cn.itcast.db; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import cn.itcast.domain.Person; import cn.itcast.service.PersonService; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.SimpleCursorAdapter; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends Activity { private PersonService personService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.personService = new PersonService(this); ListView listView = (ListView) this.findViewById(R.id.listView); Cursor cursor = personService.getCursorScrollData(0, 5); SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, new String[]{"_id", "name", "amount"}, new int[]{R.id.id, R.id.name, R.id.amount}); listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListView lView = (ListView)parent; Cursor data = (Cursor)lView.getItemAtPosition(position); int personid = data.getInt(data.getColumnIndex("_id")); Toast.makeText(MainActivity.this, personid+"", 1).show(); } }); /* List<Person> persons = personService.getScrollData(0, 5); List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); for(Person person : persons){ HashMap<String, Object> item = new HashMap<String, Object>(); item.put("id", person.getId()); item.put("name", person.getName()); item.put("amount", person.getAmount()); data.add(item); } SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"id", "name", "amount"}, new int[]{R.id.id, R.id.name, R.id.amount}); listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListView lView = (ListView)parent; HashMap<String, Object> item = (HashMap<String, Object>)lView.getItemAtPosition(position); Toast.makeText(MainActivity.this, item.get("id").toString(), 1).show(); } }); */ Button button = (Button) this.findViewById(R.id.insertbutton); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ContentResolver contentResolver = getContentResolver(); Uri insertUri = Uri.parse("content://cn.itcast.providers.personprovider/person"); ContentValues values = new ContentValues(); values.put("name", "itcastliming"); values.put("amount", 100); Uri uri = contentResolver.insert(insertUri, values); Toast.makeText(MainActivity.this, "添加完成", 1).show(); } }); } } 数据 package cn.itcast.db; import cn.itcast.service.DBOpenHelper; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; public class PersonProvider extends ContentProvider { private DBOpenHelper dbOpenHelper; private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH); private static final int PERSONS = 1; private static final int PERSON = 2; static{ MATCHER.addURI("cn.itcast.providers.personprovider", "person", PERSONS); MATCHER.addURI("cn.itcast.providers.personprovider", "person/#", PERSON); } //删除person表中的所有记录 /person //删除person表中指定id的记录 /person/10 @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); int count = 0; switch (MATCHER.match(uri)) { case PERSONS: count = db.delete("person", selection, selectionArgs); return count; case PERSON: long id = ContentUris.parseId(uri); String where = "personid="+ id; if(selection!=null && !"".equals(selection)){ where = selection + " and " + where; } count = db.delete("person", where, selectionArgs); return count; default: throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString()); } } @Override public String getType(Uri uri) {//返回当前操作的数据的mimeType switch (MATCHER.match(uri)) { case PERSONS: return "vnd.android.cursor.dir/person"; case PERSON: return "vnd.android.cursor.item/person"; default: throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString()); } } @Override public Uri insert(Uri uri, ContentValues values) {// /person SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); switch (MATCHER.match(uri)) { case PERSONS: long rowid = db.insert("person", "name", values); Uri insertUri = ContentUris.withAppendedId(uri, rowid);//得到代表新增记录的Uri this.getContext().getContentResolver().notifyChange(uri, null); return insertUri; default: throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString()); } } @Override public boolean onCreate() { this.dbOpenHelper = new DBOpenHelper(this.getContext()); return false; } //查询person表中的所有记录 /person //查询person表中指定id的记录 /person/10 @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); switch (MATCHER.match(uri)) { case PERSONS: return db.query("person", projection, selection, selectionArgs, null, null, sortOrder); case PERSON: long id = ContentUris.parseId(uri); String where = "personid="+ id; if(selection!=null && !"".equals(selection)){ where = selection + " and " + where; } return db.query("person", projection, where, selectionArgs, null, null, sortOrder); default: throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString()); } } //更新person表中的所有记录 /person //更新person表中指定id的记录 /person/10 @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); int count = 0; switch (MATCHER.match(uri)) { case PERSONS: count = db.update("person", values, selection, selectionArgs); return count; case PERSON: long id = ContentUris.parseId(uri); String where = "personid="+ id; if(selection!=null && !"".equals(selection)){ where = selection + " and " + where; } count = db.update("person", values, where, selectionArgs); return count; default: throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString()); } } } 界面 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="80dip" android:layout_height="wrap_content" android:text="435" android:id="@+id/id" /> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:text="liming" android:id="@+id/name" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="45" android:id="@+id/amount" /> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加记录" android:id="@+id/insertbutton" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="80dip" android:layout_height="wrap_content" android:text="编号" /> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:text="姓名" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="存款" /> </LinearLayout> <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/listView" /> </LinearLayout>