ContentProvider 数据供应器_联系人增删查改操作
ContentProvider 有 4 中基本的操作:查询(query)、插入(insert)、更新(update)、删除(delete),都可以通过其助手 ContentResolver 实现。ContentResolver 对象可以通过 Context 对象的 getContentResolver() 方法获取。
1 public void queryData(){ 2 3 String fields[]={ContactsContract.Contacts._ID, 4 ContactsContract.Contacts.DISPLAY_NAME, 5 ContactsContract.Contacts.Data.DATA1 6 }; 7 String q_id = et_queryId.getText().toString(); 8 Log.i("Tag", "id=" + q_id); 9 int n = 0; 10 11 if(q_id != null && q_id.length()<3){ 12 try { 13 n = Integer.parseInt(q_id); 14 cursor = cr.query(contactsUri, fields, "_id=?",new String[]{q_id}, null); 15 } catch (NumberFormatException e) { 16 Toast.makeText(this, "ID错误或未指定!", Toast.LENGTH_SHORT).show(); 17 n = 0; 18 } 19 } 20 21 if(n == 0){ 22 cursor = cr.query(contactsUri, fields,null,null, null); 23 Log.i("Tag", "查询所有联系人 !"); 24 } 25 Log.i("Tag", "共有联系人: " + cursor.getCount()); 26 27 // 填充 ListView 28 if(cursor != null){ 29 SimpleCursorAdapter sca=new SimpleCursorAdapter(this,R.layout.listview_item, 30 cursor,fields,new int[]{R.id.listview_item_id,R.id.listview_item_name,R.id.listview_item_phone}); 31 lv.setAdapter(sca); 32 } 33 }
fields 数组是欲查询字段信息,其值都是引用联系人对象 Contents 的属性。q_id 变量是当用户指定 ID 查询时,输入的 ID值。当输入 ID 进行条件查询时,需要将条件传入,查询的结果会返回 Cursor 对象,直接构造 SimpleCursorAdapter 数据适配器,设置给 ListView 显示查询结果。
添加权限
 <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
 
1 import android.app.Activity; 2 import android.content.ContentResolver; 3 import android.content.ContentUris; 4 import android.content.ContentValues; 5 import android.database.Cursor; 6 import android.net.Uri; 7 import android.os.Bundle; 8 import android.provider.ContactsContract; 9 import android.provider.ContactsContract.CommonDataKinds.Phone; 10 import android.provider.ContactsContract.CommonDataKinds.StructuredName; 11 import android.provider.ContactsContract.Contacts; 12 import android.provider.ContactsContract.Data; 13 import android.provider.ContactsContract.RawContacts; 14 import android.util.Log; 15 import android.view.View; 16 import android.view.View.OnClickListener; 17 import android.widget.Button; 18 import android.widget.EditText; 19 import android.widget.ListView; 20 import android.widget.SimpleCursorAdapter; 21 import android.widget.Toast; 22 23 public class MainActivity extends Activity { 24 25 Button bt_query,bt_insert,bt_update,bt_del; 26 EditText et_queryId,et_insertName,et_insertPhone,et_updateId,et_updatePhone,et_delId; 27 ListView lv; 28 29 ContentResolver cr; 30 Uri contactsUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 31 Cursor cursor; 32 33 @Override 34 public void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 37 setContentView(R.layout.main); 38 bt_query = (Button) findViewById(R.id.search); 39 bt_insert = (Button) findViewById(R.id.insert); 40 bt_update = (Button) findViewById(R.id.update); 41 bt_del = (Button) findViewById(R.id.delete); 42 MyListener ml = new MyListener(); 43 bt_query.setOnClickListener(ml); 44 bt_insert.setOnClickListener(ml); 45 bt_update.setOnClickListener(ml); 46 bt_del.setOnClickListener(ml); 47 et_queryId = (EditText) findViewById(R.id.searchID); 48 et_insertName = (EditText) findViewById(R.id.insertName); 49 et_insertPhone = (EditText) findViewById(R.id.insertPhone); 50 et_updateId = (EditText) findViewById(R.id.updateID); 51 et_updatePhone = (EditText) findViewById(R.id.updatePhone); 52 et_delId = (EditText) findViewById(R.id.deleteID); 53 lv = (ListView) findViewById(R.id.lv); 54 55 cr = getContentResolver(); 56 } 57 58 public void queryData(){ 59 60 String fields[]={ContactsContract.Contacts._ID, 61 ContactsContract.Contacts.DISPLAY_NAME, 62 ContactsContract.Contacts.Data.DATA1 63 }; 64 String q_id = et_queryId.getText().toString(); 65 Log.i("Tag", "id=" + q_id); 66 int n = 0; 67 68 if(q_id != null && q_id.length()<3){ 69 try { 70 n = Integer.parseInt(q_id); 71 cursor = cr.query(contactsUri, fields, "_id=?",new String[]{q_id}, null); 72 } catch (NumberFormatException e) { 73 Toast.makeText(this, "ID错误或未指定!", Toast.LENGTH_SHORT).show(); 74 n = 0; 75 } 76 } 77 78 if(n == 0){ 79 cursor = cr.query(contactsUri, fields,null,null, null); 80 Log.i("Tag", "查询所有联系人 !"); 81 } 82 Log.i("Tag", "共有联系人: " + cursor.getCount()); 83 84 // 填充 ListView 85 if(cursor != null){ 86 SimpleCursorAdapter sca = new SimpleCursorAdapter( 87 this, 88 R.layout.listview_item, 89 cursor, 90 fields, 91 new int[]{R.id.listview_item_id,R.id.listview_item_name,R.id.listview_item_phone}); 92 lv.setAdapter(sca); 93 } 94 } 95 96 public void insertData(){ 97 98 String name = et_insertName.getText().toString(); 99 String phone = et_insertPhone.getText().toString(); 100 if(name == null || name.length()<0) return; 101 ContentValues cv = new ContentValues(); 102 103 // 插入新的联系人,需要 ContactID,采用下面办法获取 104 Uri rawUri = cr.insert(RawContacts.CONTENT_URI, cv); 105 long contactID = ContentUris.parseId(rawUri); //获取ID 106 107 // 插入联系人要分步完成 108 // step 1 插入姓名 109 cv.clear(); 110 cv.put(Data.RAW_CONTACT_ID, contactID); 111 cv.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); 112 cv.put(StructuredName.GIVEN_NAME, name); //联系人姓名 113 cr.insert(Data.CONTENT_URI, cv); //执行插入 114 Log.i("Tag", "插入姓名完成"); 115 116 // step 2 插入号码 117 cv.clear(); 118 cv.put(Data.RAW_CONTACT_ID, contactID); 119 cv.put(Data.MIMETYPE , Phone.CONTENT_ITEM_TYPE); 120 cv.put(Phone.NUMBER, phone); //电话号码 121 cv.put(Phone.TYPE, Phone.TYPE_MOBILE); //号码类型,移动电话 122 cr.insert(Data.CONTENT_URI, cv); //执行插入 123 Log.i("Tag", "插入号码完成"); 124 125 // 如果后面还有邮箱等其他信息,则需要另作插入操作 126 } 127 128 public void updateData(){ 129 130 String updateID = et_updateId.getText().toString(); 131 String updatePhone = et_updatePhone.getText().toString(); 132 if(updateID == null || updateID.length()<1) return; 133 134 ContentValues cv = new ContentValues(); 135 cv.put(Phone.NUMBER, updatePhone); 136 int n = cr.update(Data.CONTENT_URI, cv, "_ID=?", new String[]{updateID}); 137 Log.i("Tag", "更新记录条数 " + n); 138 } 139 140 public void delData(){ 141 142 String delID = et_delId.getText().toString(); 143 if(delID == null || delID.length()<1) return; 144 145 int n = cr.delete(Data.CONTENT_URI, "_ID=?", new String[]{delID}); 146 Log.i("Tag", "删除记录 条数:" + n); 147 } 148 149 class MyListener implements OnClickListener{ 150 151 @Override 152 public void onClick(View v) { 153 154 switch(v.getId()){ 155 case R.id.search: 156 queryData(); 157 break; 158 case R.id.update: 159 updateData(); 160 break; 161 case R.id.insert: 162 insertData(); 163 break; 164 case R.id.delete: 165 delData(); 166 break; 167 } 168 } 169 } 170 }
主界面布局文件
 
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <LinearLayout 8 android:id="@+id/linearLayout1" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" > 11 <Button 12 android:id="@+id/search" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="查询" 16 /> 17 <EditText 18 android:id="@+id/searchID" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:hint="不指定id将查询所有联系人" 22 /> 23 </LinearLayout> 24 25 <LinearLayout 26 android:id="@+id/linearLayout2" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" > 29 <Button 30 android:id="@+id/insert" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:text="插入" 34 /> 35 <EditText 36 android:id="@+id/insertName" 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:hint="姓名" 40 /> 41 <EditText 42 android:id="@+id/insertPhone" 43 android:layout_width="match_parent" 44 android:layout_height="wrap_content" 45 android:hint="号码" 46 /> 47 </LinearLayout> 48 49 <LinearLayout 50 android:id="@+id/linearLayout3" 51 android:layout_width="match_parent" 52 android:layout_height="wrap_content" > 53 <Button 54 android:id="@+id/update" 55 android:layout_width="wrap_content" 56 android:layout_height="wrap_content" 57 android:text="更新" 58 /> 59 <EditText 60 android:id="@+id/updateID" 61 android:layout_width="wrap_content" 62 android:layout_height="wrap_content" 63 android:hint="待更ID" 64 /> 65 <EditText 66 android:id="@+id/updatePhone" 67 android:layout_width="match_parent" 68 android:layout_height="wrap_content" 69 android:hint="新号码" 70 /> 71 </LinearLayout> 72 73 <LinearLayout 74 android:id="@+id/linearLayout4" 75 android:layout_width="match_parent" 76 android:layout_height="wrap_content" > 77 <Button 78 android:id="@+id/delete" 79 android:layout_width="wrap_content" 80 android:layout_height="wrap_content" 81 android:text="删除" 82 /> 83 <EditText 84 android:id="@+id/deleteID" 85 android:layout_width="match_parent" 86 android:layout_height="wrap_content" 87 android:hint="待删除ID" 88 /> 89 </LinearLayout> 90 91 <ListView android:id="@+id/lv" 92 android:layout_width="fill_parent" 93 android:layout_height="wrap_content" 94 ></ListView> 95 96 </LinearLayout>
ListView 布局文件
 
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <LinearLayout 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:orientation="horizontal" 11 > 12 <TextView 13 android:id="@+id/listview_item_id" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="" /> 17 18 <TextView 19 android:id="@+id/listview_item_name" 20 android:layout_width="100dip" 21 android:layout_height="wrap_content" 22 android:text="" 23 android:textAppearance="?android:attr/textAppearanceLarge" /> 24 25 <TextView 26 android:id="@+id/listview_item_phone" 27 android:layout_width="match_parent" 28 android:layout_height="wrap_content" 29 android:text="" 30 android:textAppearance="?android:attr/textAppearanceMedium" /> 31 32 </LinearLayout> 33 34 </LinearLayout>
 
                    
                
 
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号