您好我搜索了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
}
}
问题是,我正在获取列表,但是当我开始滚动时图像正在加载,只有在滚动后图像稳定并处于正确位置...不确定哪里做错了,我希望图像应该加载在列表加载和图像应该在正确的位置...任何帮助表示赞赏
答案 0 :(得分: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);
new LoadImage(photo).execute(contact.getMobileNo());
答案 1 :(得分:0)
您可以在适配器充气机中设置图像,以便滚动适配器 在运行时修改。你应该在活动中设置图像
答案 2 :(得分: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
希望这会对你有所帮助。