日期:2014-05-16 浏览次数:20393 次
实现下图,图片来自SD卡、数据库、网络图片
?
定义一个ListView的xml(res/layout/list_view.xml)
?
<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/h_list_view" android:layout_width="fill_parent" android:layout_height="fill_parent" />?
Activity代码
?
public class MessageListActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
setViews();
}
private void setViews() {
ListView lv = (ListView)findViewById(R.id.h_list_view);
SimpleAdapter adapter = new ImageSimpleAdapter(this, getDatas() ,R.layout.message_list
, new String[]{"icon","title","shortContent"}, new int[]{R.id.ml_icon,R.id.ml_title,R.id.ml_short_content});
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> av, View v, int position, long id) {
//处理列表项的点击事件
}
});
}
private List<Map<String,Object>> getDatas() {
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
for (int i=1; i<=12; i++) {
Map<String,Object> map = new HashMap<String,Object>();
String imagePath = "/sdcard/image_0"+i+".jpg";
if (i >= 10) imagePath = "/sdcard/image_"+i+".jpg";
map.put("icon", BitmapFactory.decodeFile(imagePath));
map.put("title", "My Title "+i);
map.put("shortContent", "my short content "+i);
list.add(map);
}
return list;
}
}
?
?
SimpleAdapter中能放置drawable中的图片,如果想放置其它来源的图片(如:SD卡、数据库、网络图片),那么需要重写它的bindView方法,这是个private方法,因此还需要重写所以调用它的方法。
?
package com.example.adapter; import java.util.List; import java.util.Map; import android.content.Context; import android.graphics.Bitmap; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Checkable; import android.widget.ImageView; import android.widget.SimpleAdapter; import android.widget.TextView; public class ImageSimpleAdapter extends SimpleAdapter { private int[] mTo; private String[] mFrom; private ViewBinder mViewBinder; private List<? extends Map<String, ?>> mData; private int mResource; private int mDropDownResource; private LayoutInflater mInflater; public ImageSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); mTo = to; mFrom = from; mData = data; mResource = mDropDownResource = resource; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public View getView(int position, View convertView, ViewGroup parent) { return createViewFromResource(position, convertView, parent, mResource); } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { return createViewFromResource(position, convertView, parent, mDropDownResource); } private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource) { View v; if (convertView == null) { v = mInflater.inflate(resource, parent, false); } else { v = convertView; } bindView(position, v); return v; } private void bindView(int position, View view) { final Map dataSet = mData.get(position); if (dataSet == null) { return; } final ViewBinder binder = mViewBinder; final String[] from = mFrom; final int[] to = mTo; final int count = to.length