gpt4 book ai didi

Android- 使用 ViewHolder 和 AsyncTask 加载联系人- 缩略图问题

转载 作者:太空狗 更新时间:2023-10-29 12:44:44 24 4
gpt4 key购买 nike

我正在创建一个自定义联系人应用程序。我使用带有 ViewHolder 设计模式的 ArrayAdapter 进行优化...由于加载缩略图需要花费大量时间,所以我使用 AsyncTask 类来加载图像,第一组我屏幕上的联系人,图片加载良好,但当我向下滚动时,同一组图片被随机重新循环用于其他联系人...我搜索了这个论坛并找到了一些解决方案,但没有一个对我有用.我将把我的代码放在下面..有人请告诉我代码中有什么问题而不是将其标记为重复 qn...

public class CustomList extends ArrayAdapter<String>{

private final Activity context;
private final ArrayList<String> displayName,demoteValue,emergency;
ArrayList<Long> contactId;

public CustomList(Activity context,ArrayList<Long> contactId,ArrayList<String> displayName,ArrayList<String> demoteValue ,
ArrayList<String> emergency) {
super(context, R.layout.list_single, displayName);
this.context = context;
this.displayName = displayName;
this.demoteValue = demoteValue;
this.emergency = emergency;
this.contactId=contactId;

}

class MyViewHolder
{
public QuickContactBadge imageView;
public TextView txtTitle;
int position;

MyViewHolder(View v){
txtTitle = (TextView) v.findViewById(R.id.txt);
imageView = (QuickContactBadge) v.findViewById(R.id.contactimageentry);
}
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView=convertView;
MyViewHolder holder=null;

if(rowView==null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_single, parent, false);
holder=new MyViewHolder(rowView);
rowView.setTag(holder);

}else{
holder= (MyViewHolder) rowView.getTag();
}

holder.txtTitle.setText(displayName.get(position));
holder.txtTitle.setPadding(5, 5, 5, 5);
//Some BLA BLA BLA work.
if(emergency.get(position).equals("1")){

holder.txtTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,25);
holder.imageView.getLayoutParams().width=getPixels(TypedValue.COMPLEX_UNIT_DIP,50);

}
//Get and set the photo-HERE LIES THE CALL TO THE PROBLEM
holder.position = position;
new LoadContactImage(getContext(),holder,position,contactId.get(position))
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);
return rowView;
}
}

这是我的异步任务的代码,它根据我在调用它时传递的 ID 加载图像

public class LoadContactImage extends AsyncTask<Object, Void, Bitmap> {

private long Path;
private MyViewHolder mHolder;
private int mposition;
Context ctx;

public LoadContactImage(Context context,MyViewHolder holder,int position,Long id) {
this.mHolder= holder;
this.Path = id;
this.mposition=position;
this.ctx= context;
}

@Override
protected Bitmap doInBackground(Object... params) {
Uri my_uri = getPhotoUri(Path);
ContentResolver cr = ctx.getContentResolver();
InputStream in = null;
try {
in = cr.openInputStream(my_uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(in);
return bitmap;
}

@Override
protected void onPostExecute(Bitmap result) {

super.onPostExecute(result);
if (result != null && mposition == mHolder.position) {
//imv.setImageBitmap(result);
mHolder.imageView.setImageBitmap(result);
}
}
}

最佳答案

弄清楚了......现在它的工作.. Simon Meyer 在评论中给出的 Soln 是正确的..我需要在调用异步任务之前将 imageView 初始化为 getView() 中的位图..这是更改在代码中

holder.position = position;

holder.imageView.setImageURI( Uri.parse("android.resource://com.vishnu.demotingprototype/drawable/ic_contact_picture"));

new LoadContactImage(getContext(),holder,position,contactId.get(position)).execute();

关于Android- 使用 ViewHolder 和 AsyncTask 加载联系人- 缩略图问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20173132/

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