0

嗨,我搜索了 SO 并尽力而为,但无法使其正常工作..我有一个列表视图,我正在加载带有图像的电话联系人。下面是我的代码

public ProfileAdapter(Activity activity, int textViewResourceId, List<Profile> listCont,int renderer, String profileType) {
    super(activity, textViewResourceId, listCont);
    this.renderer = renderer;
    this.listCont = listCont;
    this.activity = activity;
    this.profileType = profileType;
}

public class ViewHolder {
    View rowView;
    public TextView textEmailId= null;
    public TextView textContNo= null;
    public TextView text= null;
    public ImageView photo= null;
    public int position;
}

@Override 
public View getView( int position, View convertView, ViewGroup parent) { 
    //View view = convertView; 
    ViewHolder holder;

    if (convertView == null) { 
        LayoutInflater inflater = (LayoutInflater) getContext() 
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = inflater.inflate(renderer, null); 
        holder = new ViewHolder(convertView);
        holder.text = (TextView) convertView.findViewById(R.id.name); 
        holder.textContNo = (TextView) convertView.findViewById(R.id.contactno); 
        holder.textEmailId = (TextView) convertView.findViewById(R.id.emailId);  
        holder.photo = (ImageView ) convertView.findViewById(R.id.quickContactBadge1);  
        convertView.setTag(holder);
    }
    else {
        holder =(ViewHolder) convertView.getTag();       
        holder.position = position;
        Profile contact = listCont.get(position); 
        holder.text.setText(contact.getName()); 
        ImageView photo = (ImageView ) convertView.findViewById(R.id.quickContactBadge1);   
        photo.setTag(contact.getMobileNo()); 
        photo.setImageResource(R.drawable.stub_image);
        new LoadImage(photo).execute(contact.getMobileNo()); 
    } 
    return convertView;    
}

class LoadImage extends AsyncTask<String, Void, Bitmap>{ 
    // private final WeakReference<ImageView> viewReference;
    private ImageView img; 

    public LoadImage(ImageView img) { 
        //viewReference = new WeakReference<ImageView>( view );
        this.img=img;
    } 

    @Override 
    protected Bitmap doInBackground(final String... params) { 
        new QuickContactHelper(activity, img, (String) params[0]).addThumbnail(); 
        return null;
    }

    @Override 
    protected void onPostExecute(Bitmap bitmap) {
        // nothing
    }
}

问题是,我正在获取列表,但是当我开始滚动时,图像正在加载,只有在滚动图像稳定且位置正确之后,我才知道我在哪里做错了,我希望在列表加载后加载图像和图像应该在正确的位置......任何帮助表示赞赏

4

3 回答 3

0

如果 convertView == null,您似乎什么也没做。您需要在 if 和 else 中都分配图片。

if (convertView == null) { 
    LayoutInflater inflater = (LayoutInflater) (getContext() 
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)); 
    convertView = inflater.inflate(renderer, null); 

    holder = new ViewHolder(convertView);
    holder.text = (TextView) convertView.findViewById(R.id.name); 
    holder.textContNo = (TextView) convertView.findViewById(R.id.contactno); 
    holder.textEmailId = (TextView) convertView.findViewById(R.id.emailId);  
    holder.photo = (ImageView ) convertView.findViewById(R.id.quickContactBadge1);  

    convertView.setTag(holder);
}else {
    holder =(ViewHolder) convertView.getTag();       
}

holder.position = position;
Profile contact = listCont.get(position); 
holder.text.setText(contact.getName()); 

ImageView photo = (ImageView ) convertView.findViewById(R.id.quickContactBadge1);   
photo.setTag(contact.getMobileNo()); 
photo.setImageResource(R.drawable.stub_image);

新 LoadImage(照片).execute(contact.getMobileNo());

于 2012-12-24T11:35:13.483 回答
0

您在适配器充气机中设置图像,以便在运行时滚动适配器时修改。你应该在活动中设置图像

于 2012-12-24T11:43:57.407 回答
0

只有在 convertview!=null 时才启动 AsynckTask。

要解决这个问题,只需将所有这些代码放在 if 语句之外:

   holder.position = position;
   Profile contact = listCont.get(position); 
   holder.text.setText(contact.getName()); 

   ImageView photo = (ImageView ) convertView.findViewById(R.id.quickContactBadge1);   
   photo.setTag(contact.getMobileNo()); 
   photo.setImageResource(R.drawable.stub_image);
   new LoadImage(photo).execute(contact.getMobileNo());

对于图像加载,我强烈推荐这个库:

Android-Universal-Image-Loader

希望这对您有所帮助。

于 2012-12-24T17:00:51.367 回答