Skip to content Skip to sidebar Skip to footer

Recyclerview Change The Value Of My Phonenumber In Contactscontract When Scroll The View

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 t

Solution 1:

This happens because of the way RecyclerView works, and has nothing to do with the way you are accessing the contact data. A RecyclerView only creates a ViewHolder for each of the views that will fit on screen at one time, then recycles those views when they are scrolled off screen. Then the new content for that view gets applied in onBindViewHolder().

Since you may have assigned text for the ViewHolder.textHomePhone and ViewHolder.textWorkPhone text views previously, when those view holders get recycled that text is still there. Therefore, if the new contact only has a mobile number, the text for the home number and work number will still be filled out by the previous contact occupying that ViewHolder.

To fix this you need to check if the contact doesn't have a number of each type (mobile, home and work), and if so set the visibility of the corresponding TextView to View.GONE.

A simple way to do this would be to create three boolean values before your loop, then check them afterwards:

booleanhasMobile=false;
booleanhasHome=false;
booleanhasWork=false;

while (pCur.moveToNext()) {
    intphoneType= pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
    StringphoneNumber= 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);
        hasMobile = true;
    }elseif(phoneType == TYPE_HOME){
        holder.mNumberHomePhone.setText(phoneNumber);
        holder.mNumberHomePhone.setVisibility(View.VISIBLE);
        holder.textHomePhone.setVisibility(View.VISIBLE);
        hasHome = true;
    }elseif(phoneType == TYPE_WORK){
        holder.mNumberWorkPhone.setText(phoneNumber);
        holder.mNumberWorkPhone.setVisibility(View.VISIBLE);
        holder.textWorkPhone.setVisibility(View.VISIBLE);
        hasWork = true;
    }
}

if(!hasMobile) {
    holder.mNumberMobilePhone.setVisibility(View.GONE);
    holder.textMobilePhone.setVisibility(View.GONE);
}

if(!hasHome) {
    holder.mNumberHomePhone.setVisibility(View.GONE);
    holder.textHomePhone.setVisibility(View.GONE);
}

if(!hasWork) {
    holder.mNumberWorkPhone.setVisibility(View.GONE);
    holder.textWorkPhone.setVisibility(View.GONE);
}

Solution 2:

I think the problem is with the following line:

if (dataCursor.moveToFirst())

you move the data cursor to point to the first element in each call which is not necessary and this will undo the following line at the top of the function:

dataCursor.moveToPosition(position);

if you remove the if statement, it should work fine

Post a Comment for "Recyclerview Change The Value Of My Phonenumber In Contactscontract When Scroll The View"