I'm implementing an activity with recyclerview. I load all the contacts that I have in the phone (name and phone numbers). All looks ok, the problem is that when I do a scroll in the view, when I come back at the same contact this contact don't have the correct phone numbers. Is the only one that change. Name and contact phone are display correctly.
For example, one contact only have set Mobile phone. Looks ok when initialize, but if I do scroll and come back, the Home and Work are set with another numbers, and this user only have set Mobile phone
Some help will be apreciate!
This is my onBind method in the recyclerAdapter:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
dataCursor.moveToPosition(position);
holder.mTextView.setText((dataCursor.getString(1)));
ContentResolver cr = context.getContentResolver();
String contactId = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Contacts._ID));
Long photoTest = dataCursor.getLong(dataCursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
if (dataCursor.moveToFirst()) {
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
if (phones == null) {
phones.close();
} else {
while (phones.moveToNext()) {
try {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{contactId}, null);
while (pCur.moveToNext()) {
int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if(phoneType == TYPE_MOBILE){
holder.mNumberMoBilePhone.setText(phoneNumber);
holder.mNumberMoBilePhone.setVisibility(View.VISIBLE);
holder.textMobilePhone.setVisibility(View.VISIBLE);
}else if(phoneType == TYPE_HOME){
holder.mNumberHomePhone.setText(phoneNumber);
holder.mNumberHomePhone.setVisibility(View.VISIBLE);
holder.textHomePhone.setVisibility(View.VISIBLE);
}else if(phoneType == TYPE_WORK){
holder.mNumberWorkPhone.setText(phoneNumber);
holder.mNumberWorkPhone.setVisibility(View.VISIBLE);
holder.textWorkPhone.setVisibility(View.VISIBLE);
}else{}
}
} catch (NullPointerException n) {
}
}
phones.close();
}
}
if (photoTest != null) {
ContactPhotoLoaderSdk5.instance().loadPhoto(holder.mContactPhoto, photoTest);
}
}
Not sure if have to be something with thit, but this is my select:
String select = "((" + ContactsContract.Contacts.DISPLAY_NAME + " NOTNULL) AND (" + ContactsContract.Contacts.HAS_PHONE_NUMBER + " != 0 ))";