我将使用来自网络的图像构建一个列表视图。我之前也问过另一个问题:Android ListView with images from special hashmap。但现在我尝试扩展 SimpleAdapter。我使用哈希图来放置数据。然后我使用一个新的“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" />
[...]
没有错误 - 但图像视图是空白/白色!如何使用数据中定义的图像(作为字符串/ 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});
感谢您的帮助!我尝试了很多变化......但没有任何效果:(