码迷,mamicode.com
首页 > 移动开发 > 详细

学习Android之SimpleAdapter显示网络图片

时间:2014-05-11 20:35:03      阅读:506      评论:0      收藏:0      [点我收藏+]

标签:android   xml   layout   布局   

效果图:


bubuko.com,布布扣


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

要想实现显示网络图片其实很简单,使用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>












学习Android之SimpleAdapter显示网络图片,布布扣,bubuko.com

学习Android之SimpleAdapter显示网络图片

标签:android   xml   layout   布局   

原文地址:http://blog.csdn.net/u011609853/article/details/25513035

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!