转自http://lgb168.blog.163.com/blog/static/496744382011715335070/
布局文件:
- <? xml version="1.0" encoding="utf-8"?>
-
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="fill_parent"
-
android:layout_height="?android:attr/listPreferredItemHeight">
-
< ImageView android:id="@+id/photo"
-
android:layout_width="80dip"
-
android:layout_height="20dip"
-
android:layout_alignParentTop="true"
-
android:layout_alignParentBottom="true"
-
android:adjustViewBounds="true"
-
android:padding="2dip" />
-
< TextView android:id="@+id/name"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:layout_toRightOf="@+id/photo"
-
android:layout_alignParentBottom="true"
-
android:layout_alignParentRight="true"
-
android:singleLine="true"
-
android:ellipsize="marquee"
-
android:textSize="20dip" />
-
</ RelativeLayout >
在SimpleAdapter中需要一个数据源,用来存储数据的,在显示图片时我们要用HashMap<>存储一个Bitmap;但仅存取了Bitmap时在ListView中是无法显示图片的,我们需要对SimpleAdapter进行处理 。
如下是对SimpleAdaptr处理的主要代码:
java代码片段:
-
adapter.setViewBinder(new ViewBinder (){
-
publicboolean setViewValue(View view, Object data,String textRepresentation){
-
if(view instanceof ImageView && data instanceof Bitmap){
-
ImageView iv = (ImageView) view;
-
iv.setImageBitmap((Bitmap)data);
-
returntrue;
-
}else{
-
returnfalse;
-
}
-
}
-
});
可以使用Android.widget.SimpleAdapter.ViewBinder()方法来处理SimpleAdapter不直接支持的数据;
完整代码:
-
import com.ielims.DB.Database;
-
import android.app.ListActivity;
-
import android.database.Cursor;
-
import android.database.sqlite.SQLiteDatabase;
-
import android.graphics.Bitmap;
-
import android.graphics.BitmapFactory;
-
import android.os.Bundle;
-
import android.view.View;
-
import android.widget.AdapterView;
-
import android.widget.ImageView;
-
import android.widget.ListView;
-
import android.widget.SimpleAdapter;
-
import android.widget.SimpleAdapter.ViewBinder;
-
import android.widget.Toast;
-
import android.widget.AdapterView.OnItemClickListener;
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.Map;
-
-
publicclass Frm_Frends extends ListActivity {
-
-
private String[] mListRoleName = { };
-
ListView mListView = null;
-
-
ArrayList<Map<String, Object>> mData = new ArrayList<Map<String, Object>>();;
-
-
@Override
-
protectedvoid onCreate(Bundle savedInstanceState) {
-
mListView = getListView();
-
GetCommonUsers(); //获取本地数据库Role表中用户信息
-
-
SimpleAdapter adapter = new SimpleAdapter(this, mData, R.layout.frends,
-
new String[] {
-
"photo", "name"
-
}, newint[] {
-
R.id.photo, R.id.name
-
});
-
adapter.setViewBinder(new ViewBinder (){
-
publicboolean setViewValue(View view, Object data,String textRepresentation){
-
if(view instanceof ImageView && data instanceof Bitmap){
-
ImageView iv = (ImageView) view;
-
iv.setImageBitmap((Bitmap)data);
-
returntrue;
-
}else{
-
returnfalse;
-
}
-
}
-
});
-
setListAdapter(adapter);
-
-
mListView.setOnItemClickListener(new OnItemClickListener() {
-
//处理点击事件
-
publicvoid onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
-
Toast.makeText(Frm_Frends.this, "您选择了 " + mListRoleName[position],
-
Toast.LENGTH_SHORT).show();
-
}
-
});
-
super.onCreate(savedInstanceState);
-
}
-
-
/**
-
* 获取本地数据库Role表中用户信息
-
*/
-
protectedvoid GetCommonUsers()
-
{
-
String tbName = "Role";
-
//读取Role表中的用户姓名(name) 和照片(photo)两列信息
-
String[] col =new String[]{ "name", "photo"};
-
SQLiteDatabase database = (new Database(this)).openDatabase();
-
Cursor cursor = database.query(tbName, col, null, null, null, null, null);
-
int num = cursor.getCount();
-
if (num > 0) {
-
mListRoleName = new String[num];
-
int index = 0;
-
String eName = "";
-
byte[] imgArr = null;
-
Bitmap photo = null;
-
// 必须使用moveToFirst方法将记录指针移动到第1条记录的位置
-
cursor.moveToFirst();
-
do {
-
eName = cursor.getString(cursor.getColumnIndex("name"));
-
mListRoleName[index++] = eName;
-
imgArr = cursor.getBlob(cursor.getColumnIndex("photo"));
-
Map<String, Object> item = new HashMap<String, Object>();
- if(null != imgArr && imgArr.length > 0){
-
photo = BitmapFactory.decodeByteArray(imgArr, 0, imgArr.length);
-
item.put("photo", photo);
-
}
-
else{ //如果用户没有上传头像资源,就用默认图标代替
-
item.put("photo", R.drawable.icon);
-
}
-
item.put("name", eName);
-
mData.add(item);
-
} while (cursor.moveToNext());
-
}
-
}
-
}
运行结果: