Android探秘:SimpleAdapter与Bitmap的结合

来源:互联网 发布:布丁软件官网 编辑:程序博客网 时间:2024/05/20 12:51

首先我们知道,在Android中,Adapter本身是一个接口,他 派生了很多子接口,这些子接口又被很多具体的类实现,来实现具体的显示效果。本次我们主要介绍的是SimpleAdapter实现类。

SimpleAdapter类:实际上SimpleAdapter并不简单,而且他的功能非常强大,可以将List集合的多个对象封装成列表项。

这就是我们经常需要用到的功能。例如:我们在手机上显示东西的时候,每一个Item都会有 id、title、description、image之类的attribute,显然是多个attribute,我们如何将这多个Item绑定来显示在一个Item上面,这个就是我们要深入研究SimpleAdapter的原因。

下面看SimpleAdapter一个重要的构造函数:

公有构造函数

public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)


构造函数

参数
context与 SimpleAdapter 关联的运行着的视图的上下文.dataMap 的列表.列表中的每个条目对应一行.Maps 中包含所有在 from 中指定的数据.resource定义列表项目的视图布局的资源 ID.布局文件至少应该包含在 to 中定义了的名称.from与 Map 中的项目建立关联的列名的列表.to用于显示 from 中参数中的列的视图列表.这些视图应该都是 TextView 类型的. 该列表中的第 N 个视图显示从参数 from 中的第 N 列获取的值.

但是一般情况下,我们发现image都是存放在Android自身的project里面的drawable或者/res/raw文件夹里面的,也就是刚刚开始的时候已经指定了,这时候这些图片就会映射到R.java文件中,并以int的形式保存起来,那么在调用的时候当然可以使用int类型了。

但是如果我们在开始的时候并不是直接在Android的project设置这些image的话,一个显然的问题就出现了:由于不是事先在project设置的,这些image就不定自动导入到R.java文件中,当然也就不会以int类型保存了,那么此时这个函数怎么使用了。


下面我们就使用SimpleAdapter的嵌套类——SimpleAdapter.ViewBinder

该类用于 SimpleAdapter 的外部客户将适配器的值绑定到视图. 你可以使用此类将 SimpleAdapter 不支持的值绑定到视图,或者改变 SimpleAdapter 支持的视图的绑定方式.

公有方法abstract booleansetViewValue(View view, Object data, String textRepresentation)

绑定指定的数据到指定的视图.

那么下面我们就可以这样使用SimpleAdapter的这个嵌套类来实现这个特定的功能了。

public void showDialog(Context context, final String template, int mode) {ArrayList<String> templateList = getTemplateList(template, mode);LayoutInflater inflater = LayoutInflater.from(context);final View dialog = inflater.inflate(R.layout.template, null);ListView listView = (ListView) dialog.findViewById(R.id.mylist);final List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();final AlertDialog.Builder builder = new AlertDialog.Builder(context);MTemplateProcessor processor = new MTemplateProcessor();for (int i = 0; i < templateList.size(); ++i) {try {processor.init(MainActivity.s_Engine.getThemePath()+ templateList.get(i));} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}Map<String, Object> listItem = new HashMap<String, Object>();listItem.put("id", templateList.get(i));listItem.put("title", processor.getTitle(MTemplateProcessor.LANGUAGE_CODE_ENGLISH));listItem.put("description", processor.getDescription(MTemplateProcessor.LANGUAGE_CODE_ENGLISH));MBitmap mBitmap = processor.getThumbnail(MainActivity.s_Engine,MColorSpace.MPAF_RGB32_B8G8R8A8, 128, 64);if (mBitmap != null) {Bitmap bitmap = MAndroidBitmapFactory.createBitmapFromMBitmap(mBitmap, false);listItem.put("thumbnail", bitmap);try {processor.freeThumbnail(mBitmap);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}listItems.add(listItem);try {processor.unInit();} catch (Exception e) {// TODO: handle exception}}SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems,R.layout.dialog_layout, new String[] { "title", "description","thumbnail" }, new int[] { R.id.title,R.id.description, R.id.thumbnail });<span style="color:#ff0000;"><em>simpleAdapter.setViewBinder(new ViewBinder() {@Overridepublic boolean setViewValue(View view, Object data, String arg2) {// TODO Auto-generated method stubif ((view instanceof ImageView) && (data instanceof Bitmap)) {ImageView imageView = (ImageView) view;Bitmap bitmap = (Bitmap) data;imageView.setImageBitmap(bitmap);return true;}return false;}});</em></span>listView.setAdapter(simpleAdapter);listView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) {// TODO Auto-generated method stubString templateString = MainActivity.s_Engine.getThemePath()+ listItems.get(position).get("id").toString();if (template.equals("transition"))addTransition(templateString);if (template.equals("effect"))addEffect(templateString);}});if (template.equals("transition"))builder.setTitle("Select Transition");if (template.equals("effect"))builder.setTitle("Select Effect");builder.setView(dialog);builder.show();}

/res/layout/dialog_layout.xml文件如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <TextView        android:id="@+id/title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#f00" />    <TextView        android:id="@+id/description"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#0f0" />    <ImageView        android:id="@+id/thumbnail"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

/res/layout/template.xml文件如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <TextView        android:id="@+id/title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#f00" />    <TextView        android:id="@+id/description"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#0f0" />    <ImageView        android:id="@+id/thumbnail"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

上面的显示效果如下:



上面显示的在运行的时候绑定Bitmap。

0 0