SimpleAdapter 绑定外部资源
2011-07-13 13:23 o2oo 阅读(348) 评论(0) 编辑 收藏 举报通常情况下我们通过SimpleAdapter绑定数据的时候使用Resources ID直接引用资源。但是当需要绑定外部资源,比如来自网络上的,抑或存储卡上的。这时就无法引用了,那么我们就需要通过实现 SimpleAdapter.ViewBinder来动态绑定了。使用方法类似下面的步骤。
1 SimpleAdapter adapter = new SimpleAdapter(this, (List<Map<String,Object>>) tileAndIcon,R.layout.menu_item,
2 new String[]{"url","icon","item"},new int[]{R.id.menu_url,R.id.menu_icon,R.id.menu_item});
3 adapter.setViewBinder(new MyViewBinder());
4 setListAdapter(adapter);
2 new String[]{"url","icon","item"},new int[]{R.id.menu_url,R.id.menu_icon,R.id.menu_item});
3 adapter.setViewBinder(new MyViewBinder());
4 setListAdapter(adapter);
实现SimpleAdapter.ViewBinder接口
1 import android.graphics.drawable.Drawable;
2 import android.view.View;
3 import android.widget.ImageView;
4 import android.widget.SimpleAdapter;
5
6 public class MyViewBinder implements SimpleAdapter.ViewBinder{
7
8 @Override
9 public boolean setViewValue(View view, Object data,String textRepresentation) {
10
11 if((view instanceof ImageView) && (data instanceof String)){
12 ImageView img = (ImageView)view;
13 String path = (String)data;
14 img.setImageDrawable(Drawable.createFromPath(path));
15 return true;
16 }
17
18 return false;
19 }
20
21 }
2 import android.view.View;
3 import android.widget.ImageView;
4 import android.widget.SimpleAdapter;
5
6 public class MyViewBinder implements SimpleAdapter.ViewBinder{
7
8 @Override
9 public boolean setViewValue(View view, Object data,String textRepresentation) {
10
11 if((view instanceof ImageView) && (data instanceof String)){
12 ImageView img = (ImageView)view;
13 String path = (String)data;
14 img.setImageDrawable(Drawable.createFromPath(path));
15 return true;
16 }
17
18 return false;
19 }
20
21 }