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)
构造函数
参数
但是一般情况下,我们发现image都是存放在Android自身的project里面的drawable或者/res/raw文件夹里面的,也就是刚刚开始的时候已经指定了,这时候这些图片就会映射到R.java文件中,并以int的形式保存起来,那么在调用的时候当然可以使用int类型了。
但是如果我们在开始的时候并不是直接在Android的project设置这些image的话,一个显然的问题就出现了:由于不是事先在project设置的,这些image就不定自动导入到R.java文件中,当然也就不会以int类型保存了,那么此时这个函数怎么使用了。
下面我们就使用SimpleAdapter的嵌套类——SimpleAdapter.ViewBinder
该类用于 SimpleAdapter 的外部客户将适配器的值绑定到视图. 你可以使用此类将 SimpleAdapter 不支持的值绑定到视图,或者改变 SimpleAdapter 支持的视图的绑定方式.
那么下面我们就可以这样使用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。
- Android探秘:SimpleAdapter与Bitmap的结合
- Android SimpleAdapter传入Bitmap
- [Android] ListView 结合SimpleAdapter使用
- SimpleAdapter结合ListView的使用
- Android之ListView与SimpleAdapter的使用
- 【Android】ContextMenu与SimpleAdapter的应用示例
- 【Android】ListView与SimpleAdapter适配器的使用
- Android--Gridview使用SimpleAdapter加载bitmap图片
- Android的SimpleAdapter
- Android SimpleAdapter的参数
- Android SimpleAdapter的参数
- Android的Bitmap与Drawable
- [Android] Adapter:SimpleAdapter SimpleCursorAdapter ArrayAdapter 与ListView的用法
- Android学习之ListView与SimpleAdapter的使用
- Android用simpleAdapter来接收一个Bitmap显示
- Android用simpleAdapter来接收一个Bitmap显示
- android笔记26 - SimpleAdapter 动态显示Bitmap图片
- Android Base64转换图片,SimpleAdapter动态显示Bitmap图片
- 检查打开的文件是不是ELF格式的文件,如果是就输出节头字符串表的地址,并依次输出各节的名称,字符串
- matlab经典教程分享
- MySQL C++接口封装
- 回话session和cookie的区别
- 检查打开的文件是不是ELF格式的文件,提取“特别”的节中的信息
- Android探秘:SimpleAdapter与Bitmap的结合
- int* (*a)[10]
- MATLAB中常用到的绘图函数
- Objective-C语法快速参考
- 牛人的博客链接s
- 框架——是否为新版本
- Spring Security3源码分析(20)-CAS支持
- Iptables 指南
- [2710]1-4 输出菱形 (for循环)SDUT