我将使用网络上的图片构建列表视图。我之前也问过另一个问题:Android ListView with images from special hashmap。 但现在我尝试扩展SimpleAdapter。我使用hashmap来放置数据。然后我使用一个新的“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();
}
}
}
我的list.xml:
[...]
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip" />
[...]
没有错误 - 但是imageview是空白/白色!如何使用数据中定义的图像(如String / URL)填充imageview:
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});
感谢您的帮助!我尝试了很多变化......但没有任何作用:(