学习Android之SimpleAdapter显示网络图片 – Android移动开发技术文章_手机开发 – 红黑联盟

效果图:

\

此程序主要的知识点是: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

<喎�”http://www.2cto.com/kf/ware/vc/” target=”_blank” class=”keylink”>vcD4KPHA+0qrP68q1z9bP1Mq+zfjC5828xqzG5Mq1uty88rWlo6zKudPDU2ltcGxlQWRhcHRlctbQtcS3vbeoc2ltcGxlQWRhcHRlci5zZXRWaWV3QmluZGVyo6ijqaGjPC9wPgo8cD48YnI+CjwvcD4KPHA+PHByZSBjbGFzcz0=”brush:java;”>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()语句筛选就可以了。

 

既然是网络图片,那就需要权限

?
1
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

 

代码如下:

MainActivity.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
    }
}
</string,></string,></map<string,></map<string,></map<string,>

activity_main.xml

 

?
1
2
3
4
<!--?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

?
1
2
3
4
5
6
<!--?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">
</imageview></linearlayout>

 

 

 

 

来源URL:http://www.2cto.com/kf/201405/299595.html