效果图:
此程序基本的知识点是:SimpleAdapter本身是不支持网络图片的, 假设在Map.put(a,b)中 b为一个Bitmap,程序不会报红色字体,而是在控制台输出绿色的字体,例如以下
05-10 15:46:45.474: I/System.out(846): resolveUri failed on bad bitmap uri: android.graphics.Bitmap@43e40c08
要想实现显示网络图片事实上非常easy,使用SimpleAdapter中的方法simpleAdapter.setViewBinder()。
simpleAdapter.setViewBinder(new ViewBinder() {
@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
if (view instanceof ImageView && data instanceof Bitmap) {
ImageView iv = (ImageView) view;
iv.setImageBitmap((Bitmap) data);
return true;
}
return false;
}
});
new SimpleAdapter(MainActivity.this,getData(), R.layout.t, new String[] { "images" },new int[] { R.id.ImageVIew_main });
有三个參数view,data,textRepresentation。说说前两个參数,view 是你自己定义布局(如上 R.layout.t)中全部的组件。程序会遍历一边布局中的组件,data是你传的
list<map< , >>(如上getData()方法,返回一个list<map< , >>),程序会遍历每一项数据。
接下来用if()语句筛选就能够了。
既然是网络图片,那就须要权限
<uses-permission android:name="android.permission.INTERNET" />
代码例如以下:
MainActivity.java
public class MainActivity extends ActionBarActivity {
ListView listView;
List<Map<String, ?>> data;
String str[] = { "", //自己加入图片网址 h t t p ://
"",
"",
"" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.LV_main);
SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,
getData(), R.layout.t, new String[] { "images" },
new int[] { R.id.ImageVIew_main });
simpleAdapter.setViewBinder(new ViewBinder() {
@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
if (view instanceof ImageView && data instanceof Bitmap) {
ImageView iv = (ImageView) view;
iv.setImageBitmap((Bitmap) data);
return true;
}
return false;
}
});
listView.setAdapter(simpleAdapter);
}
public List<Map<String, ?>> getData() {
data = new ArrayList<Map<String, ?>>();
for (int i = 0; i < str.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("images", returnBitMap(str[i]));
data.add(map);
}
return data;
}
public Bitmap returnBitMap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/LV_main" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView> </LinearLayout>
自己定义布局文件
t.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/ImageVIew_main"
android:layout_width="80dp"
android:layout_height="80dp"/>
</LinearLayout>