gpt4 book ai didi

android - 适配器的 getView 方法中的故障

转载 作者:行者123 更新时间:2023-11-30 04:10:37 25 4
gpt4 key购买 nike

我正在显示联系人列表。一切都很好,除了设备有 1620 个联系人,所以列表滚动非常慢,甚至有时会挂起。

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

有人告诉我让 ListView 根据需要动态加载数据,还可以使用 ViewHolder 来提高效率。我已经尝试过使用 view holder,但每次它都会一次又一次地膨胀单个 View 。我的列表滚动非常流畅,但真的不知道为什么它会一次又一次地膨胀相同的 View 。请帮忙 。 我的 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;
}

我的 Imagefrom 联系方式代码,我非常确定这段代码

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() 时,您都在运行查询并通过解码数组获取位图。这就是滚动变慢的原因。

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

您还可以按照本教程使用 SimpleCursorAdapter 加载带照片的联系人。

Add contact photo to your list application

关于android - 适配器的 getView 方法中的故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10908866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com