zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    谷歌提供的工具包一些高效的技巧
    java通过当前请求得到访问者ip的工具类
    java利用commons-email发送邮件并进行封装
    在当前进程下取得当前登陆用户
    java实现Md5加密工具类
    生成随机密码的工具类
    jenkins自动化打包报错:gradle: 未找到命令
    TypeError: not all arguments converted during string formatting
    The SDK directory '/home/wangju/gitProject/Automation/D:Android_SDK' does not exist.
    CentOS7下安装安装android sdk & gradle
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/3247074.html
Copyright © 2011-2022 走看看