根据联系人姓名首字符顺序读取通讯录
1 package com.zzw.contacts; 2 3 import java.util.ArrayList; 4 5 import android.app.ListActivity; 6 import android.content.ContentResolver; 7 import android.content.Context; 8 import android.database.Cursor; 9 import android.net.Uri; 10 import android.os.Bundle; 11 import android.provider.ContactsContract; 12 import android.view.LayoutInflater; 13 import android.view.View; 14 import android.view.ViewGroup; 15 import android.widget.ArrayAdapter; 16 import android.widget.ListView; 17 import android.widget.TextView; 18 19 public class MainActivity extends ListActivity { 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 // setContentView(R.layout.activity_main); 25 26 ArrayList<Contact> contacts = new ArrayList<Contact>(); 27 readContacts(contacts); 28 29 ListView listView = this.getListView(); 30 31 ArrayAdapter<Contact> adapter = new MyAdapter(this, android.R.layout.simple_list_item_2, contacts); 32 listView.setAdapter(adapter); 33 } 34 35 private class MyAdapter extends ArrayAdapter<Contact> { 36 37 private int resource; 38 private LayoutInflater inflater; 39 private ArrayList<Contact> contacts; 40 41 public MyAdapter(Context context, int resource, ArrayList<Contact> contacts) { 42 super(context, resource, contacts); 43 this.resource = resource; 44 this.contacts = contacts; 45 inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 46 } 47 48 @Override 49 public int getCount() { 50 return contacts.size(); 51 } 52 53 @Override 54 public Contact getItem(int position) { 55 return contacts.get(position); 56 } 57 58 @Override 59 public View getView(int position, View convertView, ViewGroup parent) { 60 61 if (convertView == null) { 62 convertView = inflater.inflate(resource, null); 63 } 64 65 Contact contact = getItem(position); 66 67 TextView text1 = (TextView) convertView.findViewById(android.R.id.text1); 68 TextView text2 = (TextView) convertView.findViewById(android.R.id.text2); 69 70 // 首字符,分组的依据 71 text1.setText(contact.firstLetterOfName()); 72 73 // 详情 74 text2.setText(contact.name + "" + contact.getPhoneNumbers()); 75 return convertView; 76 } 77 78 } 79 80 // 读取设备联系人的一般方法。大致流程就是这样,模板化的操作代码。 81 private void readContacts(ArrayList<Contact> contacts) { 82 Uri uri = Uri.parse("content://com.android.contacts/contacts"); 83 ContentResolver resolver = this.getContentResolver(); 84 85 // 在这里我们给query传递进去一个SORT_KEY_PRIMARY 86 // 告诉ContentResolver获得的结果按照联系人名字的首字母有序排序 87 Cursor cursor = resolver.query(uri, null, null, null, 88 android.provider.ContactsContract.Contacts.SORT_KEY_PRIMARY); 89 90 while (cursor.moveToNext()) { 91 // 联系人id 92 String id = cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts._ID)); 93 // Sort key,读取的联系人按照姓名从 A->Z 的排序分组 94 String sork_key_primary = cursor 95 .getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.SORT_KEY_PRIMARY)); 96 // 获取联系人姓名 97 String name = cursor 98 .getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME)); 99 100 Contact mContact = new Contact(); 101 mContact.id = id; 102 mContact.sort_key_primay = sork_key_primary; 103 mContact.name = name; 104 105 // 获取联系人的手机号码 106 Cursor phone = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 107 ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null, null); 108 // 取得电话号码(可能存在多个号码) 109 // 因为在同一个名字下,用户可能存有一个以上号码 110 // 遍历 111 ArrayList<String> phoneNumbers = new ArrayList<String>(); 112 while (phone.moveToNext()) { 113 int phoneFieldColumnIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 114 String phoneNumber = phone.getString(phoneFieldColumnIndex); 115 phoneNumbers.add(phoneNumber); 116 } 117 118 mContact.phoneNumbers = phoneNumbers; 119 contacts.add(mContact); 120 } 121 } 122 123 // 用于装载从联系人数据库中读取到的数据。 124 // 结构化数据,便于数据操作和访问。 125 private class Contact { 126 public String id; 127 public String name; 128 public String sort_key_primay; 129 public ArrayList<String> phoneNumbers; 130 131 // 获得一个联系人名字的首字母。 132 // 比如一个人的名字叫做“安卓”,那么这个人的首字母是:A 133 public String firstLetterOfName() { 134 String s = sort_key_primay.charAt(0) + ""; 135 136 return s.toUpperCase(); 137 } 138 139 public String getPhoneNumbers() { 140 String phones = ""; 141 for (int i = 0; i < phoneNumbers.size(); i++) { 142 phones += ": " + phoneNumbers.get(i); 143 } 144 return phones; 145 } 146 } 147 }
权限:
1 <!-- 写权限 --> 2 <uses-permission android:name="android.permission.WRITE_CONTACTS" /> 3 <!-- 读权限 --> 4 <uses-permission android:name="android.permission.READ_CONTACTS" />