0

我已经搜索并尝试了很多不同的方法来在 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 });

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

4

2 回答 2

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();
                }

            }
于 2012-09-12T13:39:59.510 回答
0

这绝对会帮助你,

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;

}

让我知道问题是否仍然存在..

于 2012-09-12T12:03:01.927 回答