package com.yin.listView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
public
class MainActivity
extends Activity {
ListView mListView;
View mView;
public
static
final String p_w_picpathUrl =
"http://www.google.com//ig/p_w_picpaths/weather/mostly_sunny.gif";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mView = LayoutInflater.from(this).inflate(R.layout.list_item,null);
mListView = (ListView) findViewById(R.id.mList);
List<HashMap<String,Object>> mListData = getListData();
SimpleAdapter adapter = new SimpleAdapter(this, mListData, R.layout.list_item,
new String[]{"icon"}, new int[]{R.id.p_w_picpath});
adapter.setViewBinder(new ViewBinder() {
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;
}else
return false;
}
});
mListView.setAdapter(adapter);
}
public List<HashMap<String,Object>> getListData(){
List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map = null;
for(int i=0;i<5;i++){
map = new HashMap<String, Object>();
map.put("icon",getBitmap());
list.add(map);
}
return list;
}
public Bitmap getBitmap(){
Bitmap mBitmap = null;
try {
URL url = new URL(p_w_picpathUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream is = conn.getInputStream();
mBitmap = BitmapFactory.decodeStream(is);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return mBitmap;
}
}