Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 學習Android之SimpleAdapter顯示網絡圖片

學習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
<喎?/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>(如上getData()方法,返回一個list>),程序會遍歷每一項數據。接下來用if()語句篩選就可以了。


既然是網絡圖片,那就需要權限

 

代碼如下:

MainActivity.java

public class MainActivity extends ActionBarActivity {


	ListView listView;
	List> 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> getData() {
		data = new ArrayList>();
		for (int i = 0; i < str.length; i++) {
			Map map = new HashMap();

			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



 

自定義布局文件

t.xml



    
    














  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved