Android数据存储之ContentProvider调用联系人数据

此应用分为两步开发:

第一步: 开门见山,直接使用ContentResolver查询通讯录的数据,填充进一个ListView显示

第二步: 给ListView设置上下文菜单,添加查看电话和删除联系人两个选项

 效果图:

 

先给ListView定义一个布局文件contact_list.xml

 1 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     >
 6 
 7     <TableRow >
 8         <TextView 
 9             android:id="@+id/id"    
10             android:layout_width="wrap_content"
11             android:layout_height="wrap_content"
12             android:layout_weight="1"
13             />
14         
15         <TextView 
16             android:id="@+id/name"    
17             android:layout_width="wrap_content"
18             android:layout_height="wrap_content"
19             android:layout_weight="1"
20             />
21         
22     </TableRow>    
23     
24 </TableLayout>

 

第一步: 取得各个组件,获取通讯录数据,填充ListView

注意:

for (contactsCursor.moveToFirst(); !contactsCursor.isAfterLast(); contactsCursor.moveToNext()) {...}

上句是query方法返回Cursor后逐条取出其中数据的固定模式
 1 public class MainActivity extends Activity {
 2 
 3     private TextView mainInfoText = null;
 4     private ListView contactListView = null;
 5     private List<Map<String, Object>> list = null;
 6     private SimpleAdapter adapter = null;
 7     private Cursor contactsCursor = null;
 8     
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13         
14         mainInfoText = (TextView) findViewById(R.id.mainInfoText);
15         contactListView = (ListView) findViewById(R.id.contactListView);
16         
17         ContentResolver resolver = super.getContentResolver();
18         contactsCursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
19         
20         // 当前activity取得结果集管理
21         super.startManagingCursor(contactsCursor);
22         
23         list = new ArrayList<Map<String,Object>>();
24         for (contactsCursor.moveToFirst(); !contactsCursor.isAfterLast(); contactsCursor.moveToNext()) {
25             Map<String, Object> map = new HashMap<String, Object>();
26             map.put("_id", contactsCursor.getInt(contactsCursor.getColumnIndex(ContactsContract.Contacts._ID)));
27             map.put("name", contactsCursor.getString(contactsCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
28             list.add(map);
29         }
30         adapter = new SimpleAdapter(MainActivity.this, list, R.layout.contact_list, new String[] {"_id", "name"}, new int[] {R.id.id, R.id.name});
31         contactListView.setAdapter(adapter);
       // 注册上下文菜单
32 this.registerForContextMenu(contactListView); 33 }

 

第二步:

复写二个方法: onCreateContextMenu()

1 @Override
2     public void onCreateContextMenu(ContextMenu menu, View v,
3             ContextMenuInfo menuInfo) {
4         super.onCreateContextMenu(menu, v, menuInfo);
5         menu.setHeaderTitle("操作选项");
      //添加按钮选项
6 menu.add(Menu.NONE, Menu.FIRST + 1, 1, "查看电话"); 7 menu.add(Menu.NONE, Menu.FIRST + 2, 1, "删除联系人"); 8 }

 

与onContextItemSelected()

 1     @Override
 2     public boolean onContextItemSelected(MenuItem item) {
 3         // 将菜单信息作如下转型, 可获取所长按的ListView中元素position
 4         AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
 5         int position = info.position;
 6         // 由position获取所长按联系人的id
 7         String _id = list.get(position).get("_id").toString();
 8         
 9         // 判断点击了哪个操作选项
10         switch (item.getItemId()) {
11         case Menu.FIRST + 1:
12             // 写出选择条件
13             String selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?";
14             String[] querySelectionArgs = new String[] { _id };
15             
16             // 查出符合选择条件id的对应号码
17             Cursor telephoneNoCursor = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
18                     null, selection, querySelectionArgs, null);
19             StringBuffer result = new StringBuffer("Tel: ");
20             // 将号码填充进StringBuffer中
21             for(telephoneNoCursor.moveToFirst(); !telephoneNoCursor.isAfterLast(); telephoneNoCursor.moveToNext()) {
22                 // 注意下面要使用getString而不能用getInt, 否则取到的值会出错
23                 result.append(telephoneNoCursor.getString(telephoneNoCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) + ",");
24             }
25             Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_SHORT).show();
26             break;
27             
28         case Menu.FIRST + 2:
29             // 执行删除动作
30             super.getContentResolver().delete(Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, _id), null, null);
31             // 从list中除去
32             this.list.remove(position);
33             // 通知adapter进行改变
34             this.adapter.notifyDataSetChanged();
35             Toast.makeText(MainActivity.this, "已经删除数据", Toast.LENGTH_SHORT).show();
36             break;
37         }
38         return super.onContextItemSelected(item);
39     }
40     
41 }
posted @ 2013-05-06 15:59  来杯冰镇魔卡  阅读(436)  评论(0编辑  收藏  举报