要取得通讯录里的数据,则是使用ContentResolver对象(content),以content.query的方式取出所有通讯录里的联系人,并以Cursor的方式取得其存储内容(电话、姓名等)。在SDK2.1之后的联系人设置,可有多组电话"类型"与"电话",所使用的类对象为android.provider. Contacts,写法与前几版SDK有很大差别。最后设计AutoCompleteTextView. OnItemClickListener事件,这也是当User单击联系人姓名之后,所拦截的事件处理,在其中便以Contacts- Adapter.getCursor()方法取得联系人的电话号码。
Java代码:
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.widget.AdapterView;
import android.widget.AutoCompleteTextView;
public class EX05_09 extends Activity {
private AutoCompleteTextView myAutoCompleteTextView;
private TextView myTextView1;
private Cursor contactCursor;
private ContactsAdapter myContactsAdapter;
/* 要捞出通讯录的字段 */
public static final String[] PEOPLE_PROJECTION = new String[] {
ContactsContract.Contacts._ID, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.Contacts.DISPLAY_NAME
};
/*
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.myAutoCompleteTextView);
myTextView1 = (TextView) findViewById(R.id.myTextView1);
/* 取得ContentResolver */
ContentResolver content = getContentResolver();
/* 取得通讯录的Cursor */
contactCursor = content.query ( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PEOPLE_PROJECTION, null, null, "" );
/* 将Cursor传入自己实现的ContactsAdapter */
myContactsAdapter = new ContactsAdapter(this, contactCursor); myAutoCompleteTextView.setAdapter(myContactsAdapter); myAutoCompleteTextView.setOnItemClickListener ( new AdapterView.OnItemClickListener() {
@Override
public void onItemClick (AdapterView arg0, View arg1, int arg2, long arg3) {
/* 取得Cursor */
Cursor c = myContactsAdapter.getCursor();
/* 移到所点击的位置 */
c.moveToPosition(arg2);
String number = c.getString ( c.getColumnIndexOrThrow (ContactsContract.CommonDataKinds.Phone.NUMBER) );
/* 当找不到电话时显示无输入电话 */
numbernumber = number == null ? "无输入电话" : number; myTextView1.setText ( c.getString ( c.getColumnIndexOrThrow (ContactsContract.Contacts.DISPLAY_NAME) )+ "的电话是" + number );
}
} );
}
}
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
public class ContactsAdapter extends CursorAdapter {
private ContentResolver mContent;
public ContactsAdapter(Context context, Cursor c) {
super(context, c);
mContent = 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 View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
final TextView view = (TextView)inflater.inflate (android.R.layout.simple_dropdown_item_1line, parent, false);
view.setText ( cursor.getString ( cursor.getColumnIndexOrThrow (ContactsContract.Contacts.DISPLAY_NAME) ) );
return view;
}
@Override
public String convertToString(Cursor cursor) {
return cursor.getString ( cursor.getColumnIndexOrThrow (ContactsContract.Contacts.DISPLAY_NAME) );
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (getFilterQueryProvider() != null) {
return getFilterQueryProvider().runQuery(constraint);
}
StringBuilder buffer = null;
String[] args = null;
if (constraint != null) {
buffer = new StringBuilder();
buffer.append("UPPER(");
buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
buffer.append(") GLOB ?");
args = new String[] {
constraint.toString().toUpperCase() + "*"
};
}
return mContent.query ( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, EX05_09.PEOPLE_PROJECTION, buffer == null ? null : buffer.toString(), args, "" );
}
}