如何在活动中获取Android手机上的所有联系人?

如何在活动中获取Android手机上的所有联系人?,android,android-contacts,Android,Android Contacts,如何在活动中获取Android手机上的所有联系人 建立联系班 从ContentResolver获取所有联系人列表 解析联系人列表并填充到ArrayList 使接触阵列自适应 接触阵列适配样品 //////////////////////////////////////////////////////////////////////////// private class ContactsAdapter extends ArrayAdapter<Contact> { priv

如何在活动中获取Android手机上的所有联系人

建立联系班 从ContentResolver获取所有联系人列表 解析联系人列表并填充到ArrayList 使接触阵列自适应 接触阵列适配样品

////////////////////////////////////////////////////////////////////////////
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;
    }

}

我在谷歌上找到了上述来源,但我不记得参考网站在哪里。对不起

使用以下代码获取使用ContentResolver的所有联系人


是的。。我可以取得联系,但无法设置为ArrayAdapter帮助我
ListView contactList = (ListView) findViewById(R.id.list_contact);
ContactsAdapter adapter = new ContactsAdapter(YourActivity.this,
            R.layout.list_contact, getContactList());
contactList.setAdapter(adapter);
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();
                }
            }
        }