0

URL からの画像を ListView に表示するためにさまざまな方法を検索して試しましたが、コードを機能させることができません。以下は、画像をダウンロードして 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 });

私のアプリは画像以外のすべてを表示します。Google でこの問題を検索しましたが、何の助けも得られませんでした。何か助けていただければ幸いです。

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 に答える