Android ListView控件显示数据库中图片

36 篇文章 0 订阅
10 篇文章 0 订阅

转自http://lgb168.blog.163.com/blog/static/496744382011715335070/

布局文件:

  1.   <? xml version="1.0" encoding="utf-8"?>
  2. < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="?android:attr/listPreferredItemHeight">           
  5.     < ImageView android:id="@+id/photo"
  6.         android:layout_width="80dip"
  7.         android:layout_height="20dip"
  8.         android:layout_alignParentTop="true"
  9.         android:layout_alignParentBottom="true"
  10.         android:adjustViewBounds="true"
  11.         android:padding="2dip" />
  12.     < TextView android:id="@+id/name"
  13.         android:layout_width="wrap_content"
  14.         android:layout_height="wrap_content"
  15.         android:layout_toRightOf="@+id/photo"
  16.         android:layout_alignParentBottom="true"
  17.         android:layout_alignParentRight="true"
  18.         android:singleLine="true"
  19.         android:ellipsize="marquee"
  20.         android:textSize="20dip" />   
  21. </ RelativeLayout >  

在SimpleAdapter中需要一个数据源,用来存储数据的,在显示图片时我们要用HashMap<>存储一个Bitmap;但仅存取了Bitmap时在ListView中是无法显示图片的,我们需要对SimpleAdapter进行处理 。

如下是对SimpleAdaptr处理的主要代码:

java代码片段:

  1.   adapter.setViewBinder(new ViewBinder (){
  2.        publicboolean setViewValue(View view, Object data,String textRepresentation){
  3.               if(view instanceof ImageView && data instanceof Bitmap){
  4.                     ImageView iv = (ImageView) view;
  5.                     iv.setImageBitmap((Bitmap)data);
  6.                     returntrue;
  7.                }else{
  8.                     returnfalse;
  9.                 }
  10.             }
  11.      });
 
可以使用Android.widget.SimpleAdapter.ViewBinder()方法来处理SimpleAdapter不直接支持的数据;
 完整代码:
  1. import com.ielims.DB.Database; 
  2. import android.app.ListActivity;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.AdapterView;
  10. import android.widget.ImageView;
  11. import android.widget.ListView;
  12. import android.widget.SimpleAdapter; 
  13. import android.widget.SimpleAdapter.ViewBinder;   
  14. import android.widget.Toast;
  15. import android.widget.AdapterView.OnItemClickListener;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19.  
  20. publicclass Frm_Frends extends ListActivity {
  21.  
  22.     private String[] mListRoleName = { };
  23.     ListView mListView = null;
  24.  
  25.     ArrayList<Map<String, Object>> mData = new ArrayList<Map<String, Object>>();;
  26.  
  27.     @Override
  28.     protectedvoid onCreate(Bundle savedInstanceState) {
  29.         mListView = getListView();
  30.         GetCommonUsers();  //获取本地数据库Role表中用户信息
  31.  
  32.         SimpleAdapter adapter = new SimpleAdapter(this, mData, R.layout.frends,
  33.                 new String[] {
  34.                         "photo", "name"
  35.                 }, newint[] {
  36.                         R.id.photo, R.id.name
  37.                 });   
  38.         adapter.setViewBinder(new ViewBinder (){
  39.             publicboolean setViewValue(View view, Object data,String textRepresentation){
  40.                 if(view instanceof ImageView && data instanceof Bitmap){
  41.                     ImageView iv = (ImageView) view;
  42.                     iv.setImageBitmap((Bitmap)data);
  43.                     returntrue;
  44.                 }else{
  45.                     returnfalse;
  46.                 }
  47.             }
  48.         });
  49.         setListAdapter(adapter);
  50.  
  51.         mListView.setOnItemClickListener(new OnItemClickListener() {
  52.             //处理点击事件
  53.             publicvoid onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
  54.                 Toast.makeText(Frm_Frends.this, "您选择了 "mListRoleName[position],
  55.                         Toast.LENGTH_SHORT).show();
  56.             }
  57.         }); 
  58.         super.onCreate(savedInstanceState);
  59.     }
  60.  
  61.     /**
  62.      * 获取本地数据库Role表中用户信息
  63.      */
  64.     protectedvoid GetCommonUsers()
  65.     {
  66.         String tbName = "Role";
  67.         //读取Role表中的用户姓名(name) 照片(photo)两列信息
  68.         String[] col =new String[]{ "name", "photo"};         
  69.         SQLiteDatabase database = (new Database(this)).openDatabase();
  70.         Cursor cursor = database.query(tbName, col, null, null, null, null, null);
  71.         int num = cursor.getCount(); 
  72.         if (num > 0) {
  73.             mListRoleName = new String[num];
  74.             int index = 0;
  75.             String eName = "";
  76.             byte[] imgArr = null;
  77.             Bitmap photo = null;
  78.             // 必须使用moveToFirst方法将记录指针移动到第1条记录的位置
  79.             cursor.moveToFirst();
  80.             do {
  81.                 eName = cursor.getString(cursor.getColumnIndex("name"));                
  82.                 mListRoleName[index++] = eName; 
  83.                 imgArr = cursor.getBlob(cursor.getColumnIndex("photo"));
  84.                 Map<String, Object> item = new HashMap<String, Object>();
  85.                 if(null != imgArr && imgArr.length > 0){
  86.                     photo = BitmapFactory.decodeByteArray(imgArr, 0, imgArr.length);
  87.                     item.put("photo", photo);
  88.                 }
  89.                 else{ //如果用户没有上传头像资源,就用默认图标代替
  90.                     item.put("photo", R.drawable.icon);
  91.                 
  92.                 item.put("name", eName);
  93.                 mData.add(item);
  94.             } while (cursor.moveToNext());
  95.         }
  96.     }
  97. }
 
运行结果:
 
 
Android ListView控件显示数据库中图片 - Einstein - 似水流年

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值