How could I use setViewImage in SimpleAdapter?

社会主义新天地 提交于 2019-12-11 09:41:24

问题


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:


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



来源:https://stackoverflow.com/questions/13218457/how-could-i-use-setviewimage-in-simpleadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!