`
喧嚣求静
  • 浏览: 552089 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Android用simpleAdapter来直接显示BMP图片 涉及的系列问题

阅读更多
开题前先来张效果图(也让我学会了在ITEYE使用图片嵌入文章中,有点麻烦,要先当附件上传,再引用)

一般来说,在用simpleAdapter适配器时,我们所要显示的图片资源都是程序内的本地资源,而且是以资源的ID(Resources ID)形式来表现的。
map.put("img", R.drawable.i3);
但是有时候我们需要用这个适配器来显示网上的远程图片时,该如何直接显示呢?方法是实现ViewBinder()这个接口,在里面定义数据和视图的匹配关系
 adapt.setViewBinder(new ViewBinder(){

			@Override
			public boolean setViewValue(View view, Object data,
					String textRepresentation) {
				if( (view instanceof ImageView) & (data instanceof Bitmap) ) {
					ImageView iv = (ImageView) view;
					Bitmap bm = (Bitmap) data;
					iv.setImageBitmap(bm);
					return true;
					}
					return false;

			}
    		   
    	   });

使用simpleAdapter适配器
 SimpleAdapter adapt=new SimpleAdapter(ContactInfoActivity.this,getListDate(list),R.layout.listrow,new String[]{"show_pic","nameAndSex","show_company","show_single"},
    			   new int[]{R.id.show_pic,R.id.nameAndSex,R.id.show_company,R.id.show_single});
    	   adapt.setViewBinder(new ViewBinder(){

			@Override
			public boolean setViewValue(View view, Object data,
					String textRepresentation) {
				if( (view instanceof ImageView) & (data instanceof Bitmap) ) {
					ImageView iv = (ImageView) view;
					Bitmap bm = (Bitmap) data;
					iv.setImageBitmap(bm);
					return true;
					}
					return false;

			}
    		   
    	   });
    	   this.setListAdapter(adapt);

 private List<Map<String,Object>> getListDate(List<People> list){
	   List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
	   for(People p:list){
		   Map<String, Object> map = new HashMap<String, Object>();
		   byte[] data=p.getIcon();
		   Bitmap bmp=null;
		   if (data != null && data.length > 0) {
				bmp = BitmapFactory.decodeByteArray(data, 0,
						data.length);
		   }
		   map.put("show_pic", bmp);
		   String sex=p.getGender()==1?"男":"女";
		   map.put("nameAndSex", p.getName()+"\t"+sex);
		   map.put("show_company", p.getCompany());
		   map.put("show_single", p.getSignature());
		   
		   result.add(map);
		   
	   }
	   return result;
   }

列表布局文件listrow.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    >
    <RelativeLayout android:id="@+id/relativeLayout1" android:layout_height="70dp" android:layout_width="70dp">
        <ImageView android:background="@drawable/card_default_pic"  android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/show_pic"/>
        <ImageView android:layout_width="20dp" android:src="@drawable/ico_call" android:layout_height="20dp" android:id="@+id/imageViewCall" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginLeft="50dp" android:layout_marginBottom="5dp"></ImageView>
    </RelativeLayout>
  
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" android:layout_marginLeft="7dp" >
    <TextView android:id="@+id/nameAndSex"
		android:layout_width="wrap_content" android:layout_height="wrap_content" 
	android:textSize="18sp" android:text="风一样的女人, 女" android:textColor="@color/black"/>
    <TextView android:id="@+id/show_company"
		android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp"
	android:textSize="14sp" android:text="销售员" android:textColor="@color/black"/>
    <TextView android:id="@+id/show_single"
		android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp"
	android:textSize="12sp" android:text="风一样的女人, 月饼热销中……" android:textColor="@color/black"/>
    </LinearLayout>

</LinearLayout>

Android xxxAadapter扩展阅读
  • 大小: 62.4 KB
分享到:
评论
1 楼 ddnzero 2013-12-25  
哎呀 感谢楼主的方法 ,显示一个图片真费劲哈哈

相关推荐

Global site tag (gtag.js) - Google Analytics