Recyclerview更改联系人中我的phoneNumber的值在滚动视图时进行合同 [英] Recyclerview change the value of my phoneNumber in contactsContract when scroll the view

查看:121
本文介绍了Recyclerview更改联系人中我的phoneNumber的值在滚动视图时进行合同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用recyclerview实施一项活动.我加载了电话中所有的联系人(姓名和电话号码).一切看起来都很好,问题是当我在视图中滚动时,当我回到同一联系人时,该联系人没有正确的电话号码.是唯一的变化.名称和联系电话显示正确.

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

有些帮助会很重要!

这是我在recyclerAdapter中的onBind方法:

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 ))";

推荐答案

之所以发生这种情况,是因为 RecyclerView 的工作方式,与您访问联系人数据的方式无关. RecyclerView 仅为一次可在屏幕上显示的每个视图创建一个 ViewHolder ,然后在滚动视图时回收这些视图屏幕.然后,该视图的新内容将应用到 onBindViewHolder()中.

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().

由于您之前可能已经为 ViewHolder.textHomePhone ViewHolder.textWorkPhone 文本视图分配了文本,所以当这些视图所有者被回收时,该文本仍然存在.因此,如果新联系人仅具有手机号码,则住所号码和工作号码的文本仍将由占据该 ViewHolder 的先前联系人填写.

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.

要解决此问题,您需要检查联系人没有是否具有每种类型的号码(移动电话,住所和工作地点),如果有,请设置相应的 TextView的可见性 View.GONE .

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:

boolean hasMobile = false;
boolean hasHome = false;
boolean hasWork = false;

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);
        hasMobile = true;
    }else if(phoneType == TYPE_HOME){
        holder.mNumberHomePhone.setText(phoneNumber);
        holder.mNumberHomePhone.setVisibility(View.VISIBLE);
        holder.textHomePhone.setVisibility(View.VISIBLE);
        hasHome = true;
    }else if(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);
}

这篇关于Recyclerview更改联系人中我的phoneNumber的值在滚动视图时进行合同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆