SimpleAdapter显示网络图片

项目中要显示一个由图片和文字组成的列表,但是图片显示不出来,原因是SimpleAdapter不直接支持这种类型的数据。

API中对android.widget.SimpleAdapter.ViewBinder的解释:

This class can be used by external clients of SimpleAdapter to bind values to views.You should use this class to views that are not directly supported by SimpleAdapter or to change the way binding occurs for views supported by SimpleAdapter.

所以要对SimpleAdapter做一下处理

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;  
        }  
    });  

SimpleAdapter的构造函数:
SimpleAdapter(Context context, List <? extends Map <String, ?>> data, int resource, String[] from, int[] to) 
data:所处理的数据项,比如ArrayList<HashMap<String, Object>>
resource:用到的布局文件
from:参数Map对象的key,比如new String[] { “useName”, "imgUrl"},
to:组件id,比如new int[] { R.id.text_view_user_name,R.id.image_view_icon });
一个ListView由多个列表项组成,每个列表项由一个HashMap对象提供数据,多个列表项由ArrayList对象提供多个Map对象。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值