私はリストアイテムの画像を持つリストビューを持っています。私はリストビュー用のカスタムアダプタを使用しています。 AsyncTaskを使用して、アダプタ内の各リスト項目のイメージをダウンロードしています。私のカスタムアダプタのコード: image of listview with properly loaded images しかし、私は最後の項目までスクロールし、再度最初の項目までスクロールしたときに問題が開始されます:リストビューに画像を正しく読み込む
public class AdapterUcomment extends ArrayAdapter<UserModel> {
public AdapterUcomment(Activity context, List<UserModel> list) {
super(context, R.layout.layout_comment_item, list);
this.context = context;
this.list = list;
}
class ViewHolder {
protected TextView text1, text2, text3;
protected ImageView imagev;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.layout_comment_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text1 = (TextView) view.findViewById(R.id.txtv_uname);
viewHolder.text2 = (TextView) view.findViewById(R.id.txtv_comment);
viewHolder.text3 = (TextView) view.findViewById(R.id.txtv_cmntTime);
viewHolder.imagev = (ImageView) view.findViewById(R.id.imgv_upic);
view.setTag(viewHolder);
} else {
view = convertView;
}
positionPic = position;
final ViewHolder holder = (ViewHolder) view.getTag();
String uname = list.get(position).getUname();
String ufbid = list.get(position).getUfbid();
String ucomment = list.get(position).getUcomment();
String ucomment_time = list.get(position).getUcomment_time();
holder.text1.setText(Html.fromHtml(uname));
holder.text2.setText(Html.fromHtml(ucomment));
holder.text3.setText(Html.fromHtml(ucomment_time));
// downloading the image using AsyncTask
new UserPic(position, ufbid, holder.imagev).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return view;
}
public class UserPic extends AsyncTask<String, Void, String> {
String ufbid = null;
Bitmap bitmap = null;
ImageView imageView;
int pos = 0;
public UserPic(int pos, String ufbid, ImageView imageView) {
this.ufbid = ufbid;
this.imageView = imageView;
this.pos = pos;
}
@Override
protected String doInBackground(String... strings) {
URL imageURL = null;
try {
imageURL = new URL(".......");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
imageView.setImageBitmap(bitmap);
super.onPostExecute(s);
}
}
すべてがこのような一見正常に動作しているようです最初のリスト項目イメージは次のように消えます。image of listview with improperly loaded image これを取り除き、リストビューで正しく画像を正しく読み込むにはどうしたらいいですか? ありがとうございます。
画像ローダーライブラリを使用してください:ここで
はピカソの詳細情報です。リンク:https://github.com/nostra13/Android-Universal-Image-Loader –
質問はbazillion回尋ねられました...それは再利用ビュー... 1. getViewが位置1で呼び出されたため、asynctask1を開始しています。2. getViewはasynctask2が終了し、右のイメージが4に設定されます。asynctask1が終了し、間違ったイメージが設定されます。プロダクションアプリの場合)URLをimageviewにタグとして保存AsyncTaskに渡されたURLと比較してビットマップを設定する前に設定していない場合** – Selvin