权限别忘记
<uses-permission android:name="android.permission.READ_CONTACTS" />
创建一个抽象类 并判断版本问题
abstract class ContactsAdapterBridge { abstract ListAdapter buildNameAdapter(Activity a); abstract ListAdapter buildPhonesAdapter(Activity a); abstract ListAdapter buildEmailAdapter(Activity a); public static final ContactsAdapterBridge INSTANCE=buildBridge(); private static ContactsAdapterBridge buildBridge() { int sdk=new Integer(Build.VERSION.SDK).intValue(); if (sdk<5) { return(new OldContactsAdapterBridge()); } return(new NewContactsAdapterBridge()); } }
实现这个类
class NewContactsAdapterBridge extends ContactsAdapterBridge { ListAdapter buildNameAdapter(Activity a) { String[] PROJECTION=new String[] { Contacts._ID, Contacts.DISPLAY_NAME,}; Cursor c=a.managedQuery(Contacts.CONTENT_URI,PROJECTION, null, null, null); return(new SimpleCursorAdapter( a, android.R.layout.simple_list_item_1, c, new String[] { Contacts.DISPLAY_NAME }, new int[] { android.R.id.text1 })); } ListAdapter buildPhonesAdapter(Activity a) { String[] PROJECTION=new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Phone.NUMBER }; Cursor c=a.managedQuery(Phone.CONTENT_URI,PROJECTION, null, null, null); return(new SimpleCursorAdapter( a, android.R.layout.simple_list_item_2, c, new String[] { Contacts.DISPLAY_NAME, Phone.NUMBER }, new int[] { android.R.id.text1, android.R.id.text2 })); } ListAdapter buildEmailAdapter(Activity a) { String[] PROJECTION=new String[] { Contacts._ID,Contacts.DISPLAY_NAME,Email.DATA}; Cursor c=a.managedQuery(Email.CONTENT_URI,PROJECTION, null, null, null); return(new SimpleCursorAdapter( a, android.R.layout.simple_list_item_2, c, new String[] { Contacts.DISPLAY_NAME, Email.DATA }, new int[] { android.R.id.text1, android.R.id.text2 })); } }
注意不同颜色就好了。
public class ContactSpinners extends ListActivity implements AdapterView.OnItemSelectedListener { private static String[] options={"Contact Names","Contact Names & Numbers","Contact Names & Email Addresses"}; private ListAdapter[] listAdapters=new ListAdapter[3]; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initListAdapters(); Spinner spin=(Spinner)findViewById(R.id.spinner); spin.setOnItemSelectedListener(this); ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, options); aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spin.setAdapter(aa); } public void onItemSelected(AdapterView<?> parent,View v, int position, long id) { setListAdapter(listAdapters[position]); } public void onNothingSelected(AdapterView<?> parent) { // ignore } private void initListAdapters() { listAdapters[0]=ContactsAdapterBridge.INSTANCE.buildNameAdapter(this); listAdapters[1]=ContactsAdapterBridge.INSTANCE.buildPhonesAdapter(this); listAdapters[2]=ContactsAdapterBridge.INSTANCE.buildEmailAdapter(this); } }
主程序就很简单了。
最后就是以前版本的查找联系人
class OldContactsAdapterBridge extends ContactsAdapterBridge { ListAdapter buildNameAdapter(Activity a) { String[] PROJECTION=new String[] { Contacts.People._ID, Contacts.PeopleColumns.NAME }; Cursor c=a.managedQuery(Contacts.People.CONTENT_URI, PROJECTION, null, null, Contacts.People.DEFAULT_SORT_ORDER); return(new SimpleCursorAdapter( a, android.R.layout.simple_list_item_1, c, new String[] { Contacts.PeopleColumns.NAME }, new int[] { android.R.id.text1 })); } ListAdapter buildPhonesAdapter(Activity a) { String[] PROJECTION=new String[] { Contacts.Phones._ID, Contacts.Phones.NAME, Contacts.Phones.NUMBER }; Cursor c=a.managedQuery(Contacts.Phones.CONTENT_URI, PROJECTION, null, null, Contacts.Phones.DEFAULT_SORT_ORDER); return(new SimpleCursorAdapter( a, android.R.layout.simple_list_item_2, c, new String[] { Contacts.Phones.NAME, Contacts.Phones.NUMBER }, new int[] { android.R.id.text1, android.R.id.text2 })); } ListAdapter buildEmailAdapter(Activity a) { String[] PROJECTION=new String[] { Contacts.ContactMethods._ID, Contacts.ContactMethods.DATA, Contacts.PeopleColumns.NAME }; Cursor c=a.managedQuery(Contacts.ContactMethods.CONTENT_EMAIL_URI, PROJECTION, null, null, Contacts.ContactMethods.DEFAULT_SORT_ORDER); return(new SimpleCursorAdapter( a, android.R.layout.simple_list_item_2, c, new String[] { Contacts.PeopleColumns.NAME, Contacts.ContactMethods.DATA }, new int[] { android.R.id.text1, android.R.id.text2 })); } }
要导入的包
import android.database.Cursor; import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.CommonDataKinds.Email; import android.provider.ContactsContract.CommonDataKinds.Phone;