-1
我怎樣才能獲取在Android手機上的所有聯繫人,在一個活動?我怎樣才能獲取在Android手機上的所有聯繫人,在一個活動?
我怎樣才能獲取在Android手機上的所有聯繫人,在一個活動?我怎樣才能獲取在Android手機上的所有聯繫人,在一個活動?
ContentResolver()
ArrayList<Contact>
ContactArrayAdapter
ContactArrayAdapter樣品
////////////////////////////////////////////////////////////////////////////
private class ContactsAdapter extends ArrayAdapter<Contact> {
private final int _resourceId;
private final LayoutInflater _inflater;
private final Context _context;
public ContactsAdapter(Context context, int textViewResourceId, List<Contact> contacts) {
super(context, textViewResourceId, contacts);
_context = context;
_resourceId = textViewResourceId;
_inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View v, ViewGroup parent) {
ViewHolder holder;
if (v == null) {
v = _inflater.inflate(_resourceId, null);
holder = new ViewHolder();
holder.tv_name = (TextView) v.findViewById(R.id.tv_name);
holder.tv_phonenumber = (TextView) v.findViewById(R.id.tv_phonenumber);
holder.iv_photoid = (ImageView) v.findViewById(R.id.iv_photo);
v.setTag(holder);
}
else {
holder = (ViewHolder) v.getTag();
}
Contact contact = getItem(position);
holder.tv_name.setText(contact.getName());
holder.tv_phonenumber.setText(contact.getPhoneNumber());
Bitmap bm = openPhoto(contact.getPhotoId());
if (bm != null) {
holder.iv_photoid.setImageBitmap(bm);
}
else {
holder.iv_photoid.setImageDrawable(getResources()
.getDrawable(R.drawable.ic_item_profile_medium));
}
return v;
}
private Bitmap openPhoto(long contactId) {
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
InputStream input = ContactsContract.Contacts
.openContactPhotoInputStream(_context.getContentResolver(),
contactUri);
if (input != null) {
return BitmapFactory.decodeStream(input);
}
return null;
}
private class ViewHolder {
ImageView iv_photoid;
TextView tv_name;
TextView tv_phonenumber;
}
}
分配適配器ListView
在onCreate()
或您的活動onResume()
。
ListView contactList = (ListView) findViewById(R.id.list_contact);
ContactsAdapter adapter = new ContactsAdapter(YourActivity.this,
R.layout.list_contact, getContactList());
contactList.setAdapter(adapter);
我發現上面的來源使用谷歌搜索,但我不記得哪裏是參考網站。對不起。
使用以下代碼獲取所有使用的聯繫人ContentResolver
。 Reference Link
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
}
pCur.close();
}
}
}
yup ..我可以聯繫但我不能設置爲ArrayAdapter幫助我 – pmrajnai13