Попробуйте ниже код:
List<ContactVO> contactVOList = new ArrayList();
private void getContacts() {
AsyncTaskRunner runner = new AsyncTaskRunner();
runner.execute();
}
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
private String resp;
ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params) {
ContactVO contactVO;
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contactVO = new ContactVO();
contactVO.setContactName(name);
Cursor phoneCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id},
null);
if (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactVO.setContactNumber(phoneNumber);
}
phoneCursor.close();
Cursor emailCursor = contentResolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCursor.moveToNext()) {
String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
contactVOList.add(contactVO);
}
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
hideProgressBar();
setContactAdapter();
}
@Override
protected void onPreExecute() {
showProgressBar();
}
}
public class ContactVO {
private String ContactImage;
private String ContactName;
private String ContactNumber;
private boolean isChecked;
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;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
@Override
public String toString() {
return ContactNumber.replace(" ","");
}
}
public class AllContactsAdapter extends RecyclerView.Adapter<AllContactsAdapter.ContactViewHolder> implements Filterable {
private List<ContactVO> contactVOList,contactListFiltered;
private Context mContext;
public AllContactsAdapter(List<ContactVO> contactVOList, Context mContext){
this.contactVOList = contactVOList;
this.contactListFiltered = 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) {
final ContactVO contactVO = contactListFiltered.get(position);
holder.tvContactName.setText(contactVO.getContactName());
holder.tvPhoneNumber.setText(contactVO.getContactNumber());
holder.cbContact.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
contactVO.setChecked(b);
}
});
if(contactVO.isChecked()){
holder.cbContact.setChecked(true);
}
else {
holder.cbContact.setChecked(false);
}
}
@Override
public int getItemCount() {
return contactListFiltered.size();
}
public List<ContactVO> getContacts(){
return contactVOList;
}
public List<ContactVO> getSelectedContacts(){
List<ContactVO> selectedContactVOList = new ArrayList<>();
for (int i = 0; i < contactVOList.size(); i++) {
if(contactVOList.get(i).isChecked()){
selectedContactVOList.add(contactVOList.get(i));
}
}
return selectedContactVOList;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
contactListFiltered = contactVOList;
} else {
List<ContactVO> filteredList = new ArrayList<>();
for (ContactVO row : contactVOList) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getContactName().toLowerCase().contains(charString.toLowerCase()) || row.getContactNumber().contains(charSequence)) {
filteredList.add(row);
}
}
contactListFiltered = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = contactListFiltered;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
contactListFiltered = (ArrayList<ContactVO>) filterResults.values;
notifyDataSetChanged();
}
};
}
public static class ContactViewHolder extends RecyclerView.ViewHolder{
ImageView ivContactImage;
TextView tvContactName;
TextView tvPhoneNumber;
CheckBox cbContact;
public ContactViewHolder(View itemView) {
super(itemView);
ivContactImage = (ImageView) itemView.findViewById(R.id.ivContactImage);
tvContactName = (TextView) itemView.findViewById(R.id.tvContactName);
tvPhoneNumber = (TextView) itemView.findViewById(R.id.tvPhoneNumber);
cbContact = itemView.findViewById(R.id.cbContact);
}
}
}
private void setContactAdapter() {
contactAdapter = new AllContactsAdapter(contactVOList, getApplicationContext());
rvContacts.setLayoutManager(new LinearLayoutManager(SendSMSActivity.this));
rvContacts.setAdapter(contactAdapter);
rlContacts.setVisibility(View.VISIBLE);
}