• SimpleAdapter


    正想着该如何实现左图片右文字。原来SimpleAdapter可以胜任这个工作。

    First, if a SimpleAdapter.ViewBinder is available, setViewValue(android.view.View, Object, String) is invoked. If the returned value is true, binding has occurred. If the returned value is false,

    the following views are then tried in order:

    • A view that implements Checkable (e.g. CheckBox). The expected bind value is a boolean.
    • TextView. The expected bind value is a string and setViewText(TextView, String) is invoked.
    • ImageView. The expected bind value is a resource id or a string and setViewImage(ImageView, int) or setViewImage(ImageView, String) is invoked.

    我不用SimpleAdapter.ViewBinder接口实现。而是用列表显示的三个规则。

     1 public class MainActivity extends ListActivity{
     2 
     3     @Override
     4     protected void onCreate(Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6 
     7         
     8          SimpleAdapter simpleAdapter = new SimpleAdapter(this, getSimpleData(),
     9                     R.layout.image_text_2,
    10                     new String[] { "img", "name" },
    11                     new int[] { R.id.img,R.id.name});
    12          
    13          this.setListAdapter(simpleAdapter);
    14     }
    15 
    16     private List<Map<String, Object>> getSimpleData() {
    17         List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
    18         Map<String, Object> item = new HashMap<String, Object>();
    19         item.put("img",R.drawable.ic_launcher);
    20         item.put("name", "刘备");
    21         data.add(item);
    22    
    23         item = new HashMap<String, Object>();
    24         item.put("img",R.drawable.ic_launcher);
    25         item.put("name", "关羽");
    26         data.add(item);
    27    
    28         item = new HashMap<String, Object>();
    29         item.put("img",R.drawable.ic_launcher);
    30         item.put("name", "张飞");
    31         data.add(item);
    32    
    33         item = new HashMap<String, Object>();
    34         item.put("img",R.drawable.ic_launcher);
    35         item.put("name", "赵云");
    36         data.add(item);
    37    
    38         return data;
    39     }
    40 }

    2.布局文件image_text_2.xml如下:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6     
     7     <ImageView  android:id="@+id/img"
     8          android:layout_width="wrap_content"
     9          android:layout_height="wrap_content"/>
    10     
    11     <TextView  android:id="@+id/name"
    12          android:layout_width="wrap_content"
    13          android:layout_height="wrap_content"
    14          android:layout_toRightOf="@id/img"/>
    15 </RelativeLayout>

    3.效果如下:

    4.网上找的ViewBinder实现,以备不时之需。

     1 SimpleAdapter notes = new SimpleAdapter(this, list,
     2         R.layout.meinungen_list_row, PARAM, new int[] { R.id.icon, R.id.name, R.id.content });
     3 notes.setViewBinder(new MyViewBinder());
     4 setListAdapter(notes);
     5 
     6 public class MyViewBinder implements ViewBinder {
     7     @Override
     8     public boolean setViewValue(View view, Object data,String textRepresentation) {        
     9         if( (view instanceof ImageView) & (data instanceof Bitmap) ) {
    10             ImageView iv = (ImageView) view;
    11             Bitmap bm = (Bitmap) data;
    12             iv.setImageBitmap(bm);
    13             return true;
    14         }
    15     return false;
    16 }
  • 相关阅读:
    PI SQL 语句
    MySQL数据库远程访问权限如何打开(两种方法)
    Ajax 实现无刷新分页
    php文件缓存方法总结
    PHP常用的缓存技术汇总
    PHP缓存机制详解
    用js判断页面刷新或关闭的方法
    基于JavaScript判断浏览器到底是关闭还是刷新(超准确)
    jquery刷新页面
    一些JavaScript基本函数
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/3247074.html
Copyright © 2020-2023  润新知