Android get phone number from contact list

前端 未结 4 706
耶瑟儿~
耶瑟儿~ 2020-12-21 10:29

I have these codes which basically use a ListView to display the names in the contact list and I want to get their phone number when click each single name:

         


        
4条回答
  •  生来不讨喜
    2020-12-21 11:07

    You can use below code for getting contact list in Recyclerview;

      List contactVOList = new ArrayList();
    
      String[] projection = new String[]{ContactsContract.Contacts._ID, ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.PHOTO_URI};
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        List userList = new ArrayList<>();
    
        String lastPhoneName = " ";
        if (phones.getCount() > 0) {
            while (phones.moveToNext()) {
                String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                String contactId = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));
                String photoUri = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
                if (!name.equalsIgnoreCase(lastPhoneName)) {
                    lastPhoneName = name;
                    ContactVO user = new ContactVO();
                    user.setContactName(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
                    user.setContactNumber(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                    userList.add(user);
                    Log.d("getContactsList", name + "---" + phoneNumber + " -- " + contactId + " -- " + photoUri);
                }
            }
        }
        phones.close();
    
        AllContactsAdapter contactAdapter = new AllContactsAdapter(userList, getApplicationContext());
        rvContacts.setLayoutManager(new LinearLayoutManager(PhoneDirectoryActivity.this));
        rvContacts.setAdapter(contactAdapter);
    

    Below ContactVO class file;

    public class ContactVO
    {
    private String ContactImage;
    private String ContactName;
    private String ContactNumber;
    
    public String getContactImage() {
        return ContactImage;
    }
    
    public void setContactImage(String contactImage) {
        this.ContactImage = ContactImage;
    }
    
    public String getContactName() {
        return ContactName;
    }
    
    public void setContactName(String contactName) {
        ContactName = contactName;
    }
    
    public String getContactNumber() {
        return ContactNumber;
    }
    
    public void setContactNumber(String contactNumber) {
        ContactNumber = contactNumber;
    }
    }
    

    and below is AllContactsAdapter file

        public class AllContactsAdapter extends RecyclerView.Adapter {
    
        private List contactVOList;
        private Context mContext;
        private SparseBooleanArray itemStateArray = new SparseBooleanArray();
    
        public AllContactsAdapter(List contactVOList, Context mContext) {
            this.contactVOList = contactVOList;
            this.mContext = mContext;
        }
    
        @Override
        public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.single_contact_view, null);
            ContactViewHolder contactViewHolder = new ContactViewHolder(view);
            return contactViewHolder;
        }
    
        @Override
        public void onBindViewHolder(ContactViewHolder holder, int position) {
            ContactVO contactVO = contactVOList.get(position);
            holder.tvContactName.setText(contactVO.getContactName());
            holder.tvPhoneNumber.setText(contactVO.getContactNumber());
    
            holder.bind(position);
    
            holder.cbContact.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int adapterPosition = position;
                    if (!itemStateArray.get(adapterPosition, false)) {
                        holder.cbContact.setChecked(true);
                        contactsList.add(holder.tvPhoneNumber.getText().toString());
                        itemStateArray.put(adapterPosition, true);
                    } else {
                        holder.cbContact.setChecked(false);
                        itemStateArray.put(adapterPosition, false);
                        contactsList.remove(holder.tvPhoneNumber.getText().toString());
                    }
                }
            });
        }
    
        @Override
        public int getItemCount() {
            return contactVOList.size();
        }
    
        public class ContactViewHolder extends RecyclerView.ViewHolder {
    
            ImageView ivContactImage;
            TextView tvContactName;
            TextView tvPhoneNumber;
            CheckBox cbContact;
    
            public ContactViewHolder(View itemView) {
                super(itemView);
                ivContactImage = itemView.findViewById(R.id.ivContactImage);
                tvContactName = itemView.findViewById(R.id.tvContactName);
                tvPhoneNumber = itemView.findViewById(R.id.tvPhoneNumber);
                cbContact = itemView.findViewById(R.id.cbContact);
            }
            void bind(int arg1) {
                // use the sparse boolean array to check
                if (!itemStateArray.get(arg1, false)) {
                    cbContact.setChecked(false);
                } else {
                    cbContact.setChecked(true);
                }
            }
        }
    }
    

提交回复
热议问题