【问题标题】:How can I fetch all contacts on an Android phone, in an Activity? [closed]如何在 Activity 中获取 Android 手机上的所有联系人? [关闭]
【发布时间】:2014-06-26 05:26:45
【问题描述】:

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

【问题讨论】:

    标签: android android-contacts


    【解决方案1】:
    1. 创建一个联系人类
    2. ContentResolver()获取所有联系人列表
    3. 解析联系人列表并填写到ArrayList<Contact>
    4. 制作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;
        }
    
    }
    

    将适配器分配给您活动的onCreate()onResume() 中的ListView

    ListView contactList = (ListView) findViewById(R.id.list_contact);
    ContactsAdapter adapter = new ContactsAdapter(YourActivity.this,
                R.layout.list_contact, getContactList());
    contactList.setAdapter(adapter);
    

    我通过谷歌搜索找到了上述来源,但我不记得参考站点在哪里。对此感到抱歉。

    【讨论】:

    • yup.. 我可以联系但我无法设置为 ArrayAdapter 帮助我
    【解决方案2】:

    使用以下代码获取所有使用 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();
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      相关资源
      最近更新 更多