创建PeopleProvider.java:
package com.example.ch9; import com.example.db.DbHelper; 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; import android.text.TextUtils; public class PeopleProvider extends ContentProvider { private static final int ITEMS = 1; private static final int ITEM_ID = 2; public static final String DbName = "Db_People"; public static final String TableName = "tb_people"; DbHelper dbhelper ; SQLiteDatabase db; public static final String CONTENT_ITEMS_TYPE = "vnd.android.cursor.items/com.example.ch94.Db_People"; public static final String CONTENT_ITEMID_TYPE = "vnd.android.cursor.itemid/com.example.ch94.Db_People"; public static final Uri CONTENT_URI = Uri.parse("content://com.example.ch94.Db_People/tb_people"); private static final UriMatcher sMatcher; static{ sMatcher = new UriMatcher(UriMatcher.NO_MATCH); sMatcher.addURI("com.example.ch94.Db_People", TableName, ITEMS); sMatcher.addURI("com.example.ch94.Db_People", TableName+"/#", ITEM_ID); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { db = dbhelper.getWritableDatabase(); int count = 0; switch(sMatcher.match(uri)){ case ITEMS: count = db.delete("tb_people", selection, selectionArgs); break; case ITEM_ID: String id = uri.getPathSegments().get(1); count = db.delete("tb_people", "_ID="+id+(!TextUtils.isEmpty("_ID=?")?"AND("+selection+')':""), selectionArgs); break; default: throw new IllegalArgumentException("Unknown URI"+uri); } getContext().getContentResolver().notifyChange(uri, null); return count; } @Override public String getType(Uri uri) { switch(sMatcher.match(uri)){ case ITEMS: return CONTENT_ITEMS_TYPE; case ITEM_ID: return CONTENT_ITEMID_TYPE; default: throw new IllegalArgumentException("Unknown URI"+uri); } } @Override public Uri insert(Uri uri, ContentValues values) { db = dbhelper.getWritableDatabase(); long rowId ; if(sMatcher.match(uri)!=ITEMS){ throw new IllegalArgumentException("Unknown URI"+uri); } rowId = db.insert("tb_people", "_ID", values); if(rowId>0){ Uri noteUri = ContentUris.withAppendedId(CONTENT_URI, rowId); getContext().getContentResolver().notifyChange(noteUri, null); return noteUri; } throw new IllegalArgumentException("Unknown URI"+uri); } @Override public boolean onCreate() { dbhelper = new DbHelper(this.getContext(),"Db_People",null,1); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { db = dbhelper.getReadableDatabase(); Cursor c; switch(sMatcher.match(uri)){ case ITEMS: c = db.query("tb_people", projection, selection, selectionArgs, null, null, null); break; case ITEM_ID: String id = uri.getPathSegments().get(1); c = db.query("tb_people", projection, "_ID="+id+(!TextUtils.isEmpty(selection)?"AND("+selection+')':""), selectionArgs, null, null, sortOrder); break; default: throw new IllegalArgumentException("Unknown URI"+uri); } c.setNotificationUri(getContext().getContentResolver(), uri); return c; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { db = dbhelper.getWritableDatabase(); int count = 0; switch(sMatcher.match(uri)){ case ITEMS: count = db.update("tb_people", values, selection, selectionArgs); break; case ITEM_ID: String id = uri.getPathSegments().get(1); count = db.update("tb_people", values, "_ID="+id+(!TextUtils.isEmpty(selection)?"AND("+selection+')':""), selectionArgs); break; default: throw new IllegalArgumentException("Unknown URI"+uri); } getContext().getContentResolver().notifyChange(uri, null); return count; } }
package com.example.ch94; import com.example.baseexample.R; import com.example.db.DbHelper; import android.app.Activity; import android.content.ContentResolver; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; public class SqlMainActivity extends Activity { private ListView list_people; private ContentResolver contentResolver; private Uri CONTENT_URI = Uri.parse("content://com.example.ch94.Db_People/tb_people"); // private DbHelper dbhelper; // private SQLiteDatabase db; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.ch9_sqlmain); contentResolver = this.getContentResolver(); list_people = (ListView)findViewById(R.id.list_people); // dbhelper = new DbHelper(this, "Db_People", null, 1); // db = dbhelper.getReadableDatabase(); Cursor c = contentResolver.query(CONTENT_URI, new String[]{"_id","name","phone","mobile","email"}, null, null, null); SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.ch9_peoplelist, c, new String[]{"_id","name","phone","mobile","email"}, new int[]{R.id.id,R.id.name,R.id.phone,R.id.mobile,R.id.email}); this.list_people.setAdapter(adapter); this.registerForContextMenu(list_people); } public boolean onCreateOptionsMenu(Menu menu){ menu.add(Menu.NONE,Menu.FIRST+1,1,"添加").setIcon(android.R.drawable.ic_menu_add); menu.add(Menu.NONE,Menu.FIRST+1,2,"退出").setIcon(android.R.drawable.ic_menu_delete); return true; } public boolean onOptionsItemSelected(MenuItem item){ switch(item.getItemId()){ case Menu.FIRST+1: Intent intent = new Intent(); intent.setClass(SqlMainActivity.this, AddPeopleActivity.class); startActivity(intent); break; case Menu.FIRST+2:finish(); break; } return super.onOptionsItemSelected(item); } public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){ menu.setHeaderIcon(R.drawable.ic_launcher); menu.add(0,3,0,"修改"); menu.add(0, 4, 0, "删除"); } public boolean onContextItemSelected(MenuItem item){ AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo(); switch(item.getItemId()){ case 3: String name=((TextView)menuInfo.targetView.findViewById(R.id.name)).getText().toString(); String phone=((TextView)menuInfo.targetView.findViewById(R.id.phone)).getText().toString(); String mobile=((TextView)menuInfo.targetView.findViewById(R.id.mobile)).getText().toString(); String email=((TextView)menuInfo.targetView.findViewById(R.id.email)).getText().toString(); Intent intent = new Intent(); intent.setClass(SqlMainActivity.this, AddPeopleActivity.class); Bundle bundle = new Bundle(); bundle.putLong("id", menuInfo.id); bundle.putString("name", name); bundle.putString("phone", phone); bundle.putString("mobile", mobile); bundle.putString("email", email); intent.putExtras(bundle); startActivity(intent); break; case 4: contentResolver.delete(CONTENT_URI, "_ID=?", new String[]{menuInfo.id+""}); // dbhelper = new DbHelper(this,"Db_People",null,1); // db = dbhelper.getWritableDatabase(); // db.delete("tb_people", "_id=?", new String[]{menuInfo.id+""}); break; } return true; } }
package com.example.ch94; import com.example.baseexample.R; import com.example.db.DbHelper; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class AddPeopleActivity extends Activity { private EditText edt_name; private EditText edt_phone; private EditText edt_mobile; private EditText edt_email; private Button bt_save; private ContentResolver contentResolver; private Uri CONTENT_URI = Uri.parse("content://com.example.ch94.Db_People/tb_people"); String name,phone,mobile,email; // DbHelper dbhelper; // SQLiteDatabase db; Bundle bundle; protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.ch9_addpeople); contentResolver = this.getContentResolver(); edt_name=(EditText)findViewById(R.id.edt_name); edt_phone=(EditText)findViewById(R.id.edt_phone); edt_mobile=(EditText)findViewById(R.id.edt_mobile); edt_email=(EditText)findViewById(R.id.edt_email); bt_save = (Button)findViewById(R.id.bt_save); bundle = this.getIntent().getExtras(); if(bundle!=null){ edt_name.setText(bundle.getString("name")); edt_phone.setText(bundle.getString("phone")); edt_mobile.setText(bundle.getString("mobile")); edt_email.setText(bundle.getString("email")); } bt_save.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { name = edt_name.getText().toString(); phone = edt_phone.getText().toString(); mobile = edt_mobile.getText().toString(); email = edt_email.getText().toString(); ContentValues value = new ContentValues(); value.put("name", name); value.put("phone", phone); value.put("mobile", mobile); value.put("email", email); // DbHelper dbhelper = new DbHelper(AddPeopleActivity.this,"Db_People",null,1); // SQLiteDatabase db = dbhelper.getWritableDatabase(); long status; if(bundle!=null){ status = contentResolver.update(CONTENT_URI, value, "_ID=?", new String[]{bundle.getLong("id")+""}); }else{ Uri uri2 = contentResolver.insert(CONTENT_URI, value); if(uri2!=null){ status =1; }else{ status = -1; } // status = db.insert("tb_people", null, value); } if(status!=-1){ Toast.makeText(AddPeopleActivity.this, "保存成功", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(AddPeopleActivity.this, "保存失败", Toast.LENGTH_LONG).show(); } } }); } }
<provider android:name="com.example.ch9.PeopleProvider" android:authorities="com.example.ch94.Db_People"/>