How could I use setViewImage in SimpleAdapter?

2019-08-14 11:40发布

I will build a listview with images from the web. I've also asked an other question before: Android ListView with images from special hashmap. But now I tried to extend the SimpleAdapter. I use a hashmap to put the data. Then I use a new "ImageAdapter":

public class ImageAdapter extends SimpleAdapter {

         public ImageAdapter(Context context,
                    List<? extends Map<String, ?>> data, int resource, String[] from,
                    int[] to) {
                super(context, data, resource, from, to);
                // TODO Auto-generated constructor stub
            }

         @Override
            public void setViewImage(ImageView v, String value) {
                super.setViewImage(v, value);
                URL url;
                try {
                    url = new URL(value);
                    URLConnection conn = url.openConnection();
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    Bitmap bm = BitmapFactory.decodeStream(is);
                    v.setImageBitmap(bm);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

          }

My list.xml:

[...]
<ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip" />
[...]

There is no error - but the imageview is blank / white! How could I fill the imageview with the image defined in data (as String / URL):

ada = new ImageAdapter(getApplicationContext(), data, R.layout.list, new String[] {"imgurl", "title", "date", "ex", "id"}, new int[] {R.id.list_image, android.R.id.text1, android.R.id.text2, R.id.text3});

Thanks for helping! I tried many variations...but nothing works :(

1条回答
倾城 Initia
2楼-- · 2019-08-14 12:31

I recommend you to read this tutorial. You will learn about BaseAdapter, ViewHolder pattern and correct asynchronous image loading.

查看更多
登录 后发表回答