在适配器的getView方法毛刺 [英] Glitches in adapter's getView method

查看:130
本文介绍了在适配器的getView方法毛刺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显示的联系人列表。所有好除了设备有1620联系人,以便列表滚动很慢,甚至有时会挂起。

I am showing the list of contacts .All good except that device has 1620 contacts so list is scrolling very slow and even sometimes get hangs.

        I tried using a check in getView method for ConvertView!=null but it alwayz inflate same view many times. thanks in advance..

        My code for getView method:-
        if(ConvertView==null)
                {   view= mInflater.inflate(R.layout.facebookfriend, null);
                        TextView name=(TextView)view.findViewById(R.id.textView1);
                        ImageView image=(ImageView)view.findViewById(R.id.imageView1);
                    name.setText(mlist.get(position).get("name"));

                        String Id=mlist.get(position).get("contactId");
                        Log.e("Id",""+Id);
                        CheckBox chkbox= (CheckBox)view.findViewById(R.id.checkBox1);
                        chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(
                                    CompoundButton buttonView, boolean isChecked) {
                                isSelected.set(position, isChecked);
                            }
                        });

                                        String photoid=mlist.get(position).get("photoId");
                    Log.e("photoid",""+photoid);
                        if(mlist.get(position).get("photoId")!=null){
                            Log.e("photoid",""+"photoid");
                            image.setImageBitmap(loadContactPhoto(Id, mlist.get(position).get("photoId")));
                    }
                }
            }

有人告诉我,使列表视图动态地加载数据按要求,还用ViewHolder,使之更有效率。
我已经尝试过使用视图持有人,但每一次它再膨胀的单一视图和again.My名单非常流畅滚动,但真的不知道为什么它一再夸大了同样的观点。请帮忙 。
    我的code。与viewHolder: -

somebody told me to make the listview to load the data dynamically as per the requirement,also to use ViewHolder to make it more efficient. I have already tried it using view holder but it every time it inflates the single view again and again.My list is scrolling very smoothly but really do not know why it inflate the same view again and again. Please Help . My code with viewHolder :-

                    if (ConvertView == null) {
                        holder = new ViewHolder();
                        ConvertView= mInflater.inflate(R.layout.facebookfriend, null);
                        holder.name = (TextView)ConvertView.findViewById(R.id.textView1);
                        holder.imageView = (ImageView)ConvertView.findViewById(R.id.imageView1);
                        holder.chkbox= (CheckBox)ConvertView.findViewById(R.id.checkBox1);
                        ConvertView.setTag(holder);
                    } else {
                        holder = (ViewHolder) ConvertView.getTag();
                    }
                    holder.name.setText(mlist.get(position).get("Name").toString());

                    holder. chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(
                                CompoundButton buttonView, boolean isChecked) {
                            isSelected.set(position, isChecked);
                            Log.e("position"+position,""+isChecked);
                        }
                    });
                    String Id=mlist.get(position).get("Contactid").toString();

                    Log.e("Id","--------------"+Id);
                    if(mlist.get(position).get("Photoid")!=null){
                        Log.e("photoid",""+"photoid");
                        holder.imageView.setImageBitmap(loadContactPhoto(Id, mlist.get(position).get("Photoid")));
                        Log.e("position position",""+position);
                    }
                }

    static class ViewHolder {
        TextView name;
        ImageView imageView;
        CheckBox chkbox;
    }

我的code为Imagefrom的联系方式,我为这个code

My code for Imagefrom contact method,I am damn sure for this code

private Bitmap loadContactPhoto(String id, String photoId) {
        Long _id=Long.parseLong(id);
        Long _photoId=0l;
        if(photoId!=null){
            _photoId=Long.parseLong(photoId);
        }
        ContentResolver cr = Setting.this.getContentResolver();
        Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, _id);
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
        if (input != null) {
            return BitmapFactory.decodeStream(input);
        } else {
            Log.d("PHOTO", "first try failed to load photo");

        }

        byte[] photoBytes = null;
        Uri photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, _photoId);
        Cursor c = cr.query(photoUri,new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO },
                null, null, null);
        try {
            if (c.moveToFirst())
                photoBytes = c.getBlob(0);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            c.close();
        }

        if (photoBytes != null)
            return BitmapFactory.decodeByteArray(photoBytes, 0,
                    photoBytes.length);
        else
            Log.d("PHOTO", "second try also failed");
        Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.default_image);
        return icon;

}

推荐答案

您的问题是在 loadContactPhoto()我想。每次用 loadContactPhoto()您运行的查询,并通过解码阵列获取的位图。这就是为什么滚动越来越慢。

Your problem is in loadContactPhoto() I think. Each time with loadContactPhoto() you are running query and get the Bitmap by decoding array. That's why the scrolling is getting slow.

尝试加载的ListView 前装入所有联系人的图像,并让我们知道发生了什么。

Try to load all the contact images before loading the ListView and let us know what happened.

您也可以按照本教程中使用,用来加载照片联系人 SimpleCursorAdapter

You can also follow this tutorial to load contact with photo using SimpleCursorAdapter.

添加联系人照片到你的列表应用程序

这篇关于在适配器的getView方法毛刺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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