listview 异步加载网络图片问题

mafgm 2012-11-13 05:57:27
public class MapListImageAndText {
06 private String imageUrl;
07 private String shopname;
08 private String activitynifo;
09 private String address;
10 private String telephone;
11 private String distance;
12

13 public MapListImageAndText(String imageUrl, String shopname, String activitynifo, String address, String telephone,String distance) {

14 this.imageUrl = imageUrl;
15 this.shopname = shopname;
16 this.activitynifo = activitynifo;
17 this.address = address;
18 this.telephone = telephone;
19 this.distance=distance;
20 }
21
22 public String getImageUrl() {
23 return imageUrl;
24 }
25
26 public String getShopname() {
27 return shopname;
28 }
30 public String getActivitynifo() {
31 return activitynifo;
32 }
34 public String getAddress() {
35 return address;
36 }
38 public String getTelephone() {
39 return telephone;
40 }
42 public String getDistance() {
43 return distance;
44 }
-----------------------------------------------------
public class MapListViewCache {
11 private View baseView;
12 private TextView shopname;
13 private TextView activitynifo;
14 private TextView address;
15 private TextView telephone;
16 private TextView distance;
17
18 private ImageView imageView;
19
20 public MapListViewCache(View baseView) {
21 this.baseView = baseView;
22 }
23
24 public TextView getShopname() {
25 if (shopname == null) {
26 shopname = (TextView) baseView.findViewById(R.id.maplistviewitemshopname);
27 }
28 return shopname;
29 }
30
31 public TextView getActivitynifo() {
32 if (activitynifo == null) {
33 activitynifo = (TextView) baseView.findViewById(R.id.maplistviewitemActi);
34 }
35 return activitynifo;
36 }
37
38 public TextView getAddress() {
39 if (address == null) {
40 address = (TextView) baseView.findViewById(R.id.maplistviewitemaddr);
41 }
42 return address;
43 }
44
45 public TextView getTelephone() {
46 if (telephone == null) {
47 telephone = (TextView) baseView.findViewById· (R.id.maplistviewitemtelphone);
48 }
49 return telephone;
50 }
51
52 public ImageView getImageView() {
53 if (imageView == null) {
54 imageView = (ImageView) baseView.findViewById(R.id.maplistviewitemImage);
55 }
56 return imageView;
57 }
58
59 public TextView getDistance() {
60 if (distance == null) {
61 distance = (TextView) baseView.findViewById(R.id.maplistviewitemdistance);
62 }
63 return distance;
64 }
65
66 }
-----------------------------------------------------------------
public class AsyncImageLoader {
16 private HashMap<String, SoftReference<Drawable>> imageCache;
17
18 public AsyncImageLoader() {
19 imageCache = new HashMap<String, SoftReference<Drawable>>();
20 }
21
22 public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
23 if (imageCache.containsKey(imageUrl)) {
24 SoftReference<Drawable> softReference = imageCache.get(imageUrl);
25 Drawable drawable = softReference.get();
26 if (drawable != null) {
27 return drawable;
28 }
29 }

30 final Handler handler = new Handler() {
1 public void handleMessage(Message message) {
32 imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
33 }
34 };
35 new Thread() {
36 @Override
37 public void run() {
38 Drawable drawable = loadImageFromUrl(imageUrl);
39 imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
40 Message message = handler.obtainMessage(0, drawable);
41 handler.sendMessage(message);
42 }
3 }.start();
44 return null;
45 }
46
47 public static Drawable loadImageFromUrl(String url) {
48 URL m;
49 InputStream i = null;
50 try {
51 m = new URL(url);
52 i = (InputStream) m.getContent();
53 } catch (MalformedURLException e1) {
54 e1.printStackTrace();
55 } catch (IOException e) {
56 e.printStackTrace();
7 }
58 Drawable d = Drawable.createFromStream(i, "src");
59 return d;
60 }
61
62 public interface ImageCallback {
63 public void imageLoaded(Drawable imageDrawable, String imageUrl);
64 }
65

66 }
-----------------------------------------------------------------------------

public class MapListImageAndTextListAdapter extends ArrayAdapter<MapListImageAndText> {
20
21 private ListView listView;
22 private AsyncImageLoader asyncImageLoader;
23
24 public MapListImageAndTextListAdapter(Activity activity, List<MapListImageAndText> imageAndTexts, ListView listView) {
25 super(activity, 0, imageAndTexts);
26 this.listView = listView;
27 asyncImageLoader = new AsyncImageLoader();
28 }
29
30 public View getView(int position, View convertView, ViewGroup parent) {
31 Activity activity = (Activity) getContext();
32
33 // Inflate the views from XML
34 View rowView = convertView;
35 MapListViewCache viewCache;
36 if (rowView == null) {
37 LayoutInflater inflater = activity.getLayoutInflater();
38 rowView = inflater.inflate(R.layout.maplistviewitem, null);
39 viewCache = new MapListViewCache(rowView);
40 rowView.setTag(viewCache);
41 } else {
42 viewCache = (MapListViewCache) rowView.getTag();
43 }
44 MapListImageAndText imageAndText = getItem(position);
45
46 // Load the image and set it on the ImageView
47 String imageUrl = imageAndText.getImageUrl();
48 ImageView imageView = viewCache.getImageView();
49 imageView.setTag(imageUrl);
50 Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
51
52
53 public void imageLoaded(Drawable imageDrawable, String imageUrl) {
54 ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
5 if (imageViewByTag != null) {
56 imageViewByTag.setImageDrawable(imageDrawable);
57 }
58 }
59 });
60 if (cachedImage == null) {
Log.e("Adapter", "null");
61 imageView.setImageResource(R.drawable.refresh);

62 }else{
63 imageView.setImageDrawable(cachedImage);
64 }
65 // Set the text on the TextView
66 TextView shopname = viewCache.getShopname();
67 shopname.setText(imageAndText.getShopname());
68
69 TextView activitynifo = viewCache.getActivitynifo();
70 activitynifo.setText(imageAndText.getActivitynifo());
71
72 TextView address = viewCache.getAddress();
73 address.setText(imageAndText.getAddress());
74
75 TextView telephone = viewCache.getTelephone();
76 telephone.setText(imageAndText.getTelephone());
77
78 TextView distance = viewCache.getDistance();
79 distance.setText(imageAndText.getDistance());
80
81 return rowView;
82 }
83
84 }



...全文
152 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mafgm 2012-11-16
  • 打赏
  • 举报
回复
如果我在调试状态下运行,图片都能加载.但是真正运行起来就不行了,我考虑是线程的问题,但不知道到底应该修改那里
zanglengyu 2012-11-14
  • 打赏
  • 举报
回复
另外 建议你把你的 handler处理放到 loadDrawable 这个函数的外边,每次调用这个函数起一个线程,人后在处理之后再显示 逻辑混乱, 建议你一个把 要现实的图片定义成成员变量,每次在另外一个函数(新定义一个)中起线程,每次初始化完成给 成员变量赋值,并在handMessage中显示
zanglengyu 2012-11-14
  • 打赏
  • 举报
回复
因为你这个函数 loadDrawable 最终结果如果get不到就返回null ,至于为什么回事null ,你需要仔细看看线程中操作了
fameview000 2012-11-13
  • 打赏
  • 举报
回复
学习中,期待新答案
mafgm 2012-11-13
  • 打赏
  • 举报
回复
主程序中Listview与MapListImageAndTextListAdapter的捆绑 view sourceprint?01 //tuangoupoints为对后台传回来的数据解析后得到的字符串 02 String[] mtuangoupoints =tuangoupoints.split("@"); 03 04 List<MapListImageAndText> dataArray=new ArrayList<MapListImageAndText>(); 05 06 for(int i=0; i<mtuangoupoints.length;i++){ 07 String[] tonepoint=mtuangoupoints[i].split("#"); 08 09 String shopname=String.valueOf(i+1)+tonepoint[2]; 10 String activityinfo=tonepoint[1]; 11 String address=tonepoint[6]; 12 String telephone=tonepoint[7]; 13 String imageurl=tonepoint[8]; 14 String distance=tonepoint[5]; 15 16 MapListImageAndText test=new MapListImageAndText(imageurl,shopname,activityinfo,address,telephone,distance); 17 dataArray.add(test); 18 } 19 20 MapListImageAndTextListAdapter adapter=new MapListImageAndTextListAdapter(this, dataArray, mlistView); 21 mlistView.setAdapter(adapter); 这是我在网上下载的源码,但是我在运行时红色部分总是输出null,导致部分图片不能显示.比如listview加载10条记录,有时只加载3条网络上的图片,其他加载了本地图片,请高手指教

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧