这几天在完成《anbdroidSDK开发范例大全》中关于“搜索手机通讯录自动完成”的例子时,由于书上的例子是针对SDK1.5版本的,在用android2.2版本时,查询到的手机号为null,避免大家走弯路,特将代码贴出来共同探讨学习,由于个人能力有限,望各位大虾们指导。 创建autocontact.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <AutoCompleteTextView android:text="" android:id="@+id/contactACTV" android:layout_width="fill_parent" android:layout_height="wrap_content"> </AutoCompleteTextView> <TextView android:text="" android:id="@+id/contactTV" android:layout_width="fill_parent" android:layout_height="wrap_content"> </TextView> </LinearLayout> 创建ContactActivity类 public class ContactActivity extends Activity{ //自动提示框 AutoCompleteTextView autoCompleteTextView; TextView tv; //数据库查询得到的游标 Cursor cursor; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.autocontacts); //获得活动中的组件对象 autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.contactACTV); tv = (TextView)findViewById(R.id.contactTV); //查询已存在的联系人信息 并将信息绑定到autocompleteTextView中 ContentResolver cr = getContentResolver(); //制定查询字段 String[] fieldes = {ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME}; //获得查询的信息 cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, fieldes, null, null, null); while(cursor.moveToNext()) { String[] columns = cursor.getColumnNames(); for(String s: columns) { String value = cursor.getString(cursor.getColumnIndexOrThrow(s)); System.out.println(s+": "+value+"\n"); } } //绑定自动提示框信息 ContentApdater adapter = new ContentApdater(this, cursor); autoCompleteTextView.setAdapter(adapter); autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //将游标定位到要显示数据的行 cursor.move(position); String name= cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); String phone = showPhone(); String info =name+"的\n \t\t电话是:"+phone; info += "\n \t\t电子邮件:"+showEmail(); tv.setText(info); } //显示电话号码 public String showPhone() { String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID)); Cursor phoneCursor = getContentResolver().query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?", new String[]{id}, null); if(phoneCursor.moveToNext()) { String phone =phoneCursor.getString(phoneCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DATA)); return phone; } phoneCursor.close(); return null; } //显示电子邮件 public String showEmail() { String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID)); Cursor emailCursor = getContentResolver().query (ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID+"=?", new String[]{id}, null); if(emailCursor.moveToNext()) { String email = emailCursor.getString(emailCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.DATA)); return email; } return null; } }); } } class ContentApdater extends CursorAdapter{ ContentResolver resolver; //构造函数 public ContentApdater(Context context, Cursor c) { super(context, c); resolver = context.getContentResolver(); } @Override //将信息绑定到控件的方法 public void bindView(View view, Context context, Cursor cursor) { ((TextView)view).setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME))); } @Override public CharSequence convertToString(Cursor cursor) { return cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); } @Override //创建自动绑定选项 public View newView(Context context, Cursor cursor, ViewGroup parent) { final LayoutInflater inflater = LayoutInflater.from(context); final TextView tv = (TextView)inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent,false); tv.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME))); return tv; } } 看效果时记得在功能选项中,创建联系人(Contacts-->menu-->new contacts) |