【问题标题】:Android - How to display images from URL in a ListView?Android - 如何在 ListView 中显示来自 URL 的图像?
【发布时间】:2012-07-15 14:15:33
【问题描述】:

我已经搜索并尝试了很多不同的方法来在 ListView 中显示来自 URL 的图像,但我无法让我的代码正常工作。下面是我下载图像然后在 ListView 中显示它们的代码,但不知何故它不起作用。

private Drawable LoadImageFromWebOperations(String url)
    {
        try
        {
            InputStream is = (InputStream) new URL(url).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");
            return d;
        }
        catch (Exception e) 
        {
             return null;
        }
    }

这是我调用上述函数的代码

Drawable image = LoadImageFromWebOperations("http://10.0.0.5/images/logo.jpg");

在这之后,我将它放入一个 Map 中,然后将 Map 放入一个名为“productsList”的 ArrayList 中

map.put("avatar", image);
productsList.add(map);

最后我用 SimpleAdapter 在 ListView 中显示 HashMap

ListAdapter adapter = new SimpleAdapter(
   Home.this, productsList, 
   R.layout.list_item, new String[] { TAG_PID,
   TAG_NAME, "url", "avatar"},
   new int[] { R.id.pid, R.id.name, R.id.url, R.id.avatar });

我的应用会显示除图像以外的所有内容。我在谷歌上搜索过这个问题,但没有得到任何帮助。任何帮助表示赞赏?

【问题讨论】:

  • 您确认 LoadImageFromWebOperations 不返回 null 吗?
  • @Nobu 是的,我在 TextView 中打印了结果,结果类似于 android.graphics.drawable.BitmapDrawable@2bf022d0

标签: android android-listview imageview android-image


【解决方案1】:
try

if (image != null) {

                Bitmap bitimage = null;
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 1;
                try {

                                           //bitimage = BitmapFactory.decodeStream((InputStream) new URL(data.getThumbnail().toString().trim().toString()).getContent(), null, options);
                                            bitimage = BitmapFactory.decodeStream((InputStream) new URL(ed).getContent(), null, options);
                    image.setImageBitmap(bitimage);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

【讨论】:

    【解决方案2】:

    这肯定会对你有所帮助,

    CustomAdapter adapter = new CustomAdapter(Home.this, productsList, R.layout.list_item,
    new String[] { TAG_PID,TAG_NAME, "url", "avatar"},
     new int[] { R.id.pid, R.id.name, R.id.url, R.id.avatar });
    

    创建一个扩展 SimpleAdapter 的 CustomAdapter 类,覆盖 getView() 方法,

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v =  super.getView(position, convertView, parent);
        HashMap<String, Object> hm = (HashMap<String, Object>) super.getItem(position);
        ImageView image = (ImageView) v.findViewById(R.id.yourImageID);
        TextView txt1 = (TextView) v.findViewById(R.id.yourtxtID1);
        TextView txt2 = (TextView) v.findViewById(R.id.yourtxtID2);
        TextView txt3 = (TextView) v.findViewById(R.id.yourtxtID3);
    
    
        image.setImageDrawable((Drawable) hm.get("avatar"));
        txt1.setText(hm.get("TAG_PID").toString());
        txt2.setText(hm.get("TAG_NAME").toString());
        txt3.setText(hm.get("url").toString());
    
        return v;
    
    }
    

    如果问题仍然存在,请告诉我..

    【讨论】:

      猜你喜欢
      • 2014-05-20
      • 1970-01-01
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 2011-09-18
      • 2011-09-23
      • 1970-01-01
      • 2011-09-12
      相关资源
      最近更新 更多