举例讲解Android应用中SimpleAdapter简单适配器的使用

 更新时间:2016年04月24日 10:21:14   作者:剑萧舞蝶  
这篇文章主要介绍了Android应用中SimpleAdapter简单适配器的使用例子,SimpleAdapter经常在ListView被使用,需要的朋友可以参考下

SimpleAdapter,跟名字一样,一个简单的适配器,既为简单,就只是被设计来做简单的应用的,比如静态数据的绑定,不过仍然有自定义的空间,比如说在每一个ListItem中加一个按钮并添加响应事件.首先还是先看一下SimpleAdapter的定义吧,直接翻译下SDK doc 吧:

这是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图。你可以指定由Map组成的List(比如ArrayList)类型的数据。在ArrayList中的每个条目对应List中的一行。Maps包含每一行的数据。你可以指定一个XML布局以指定每一行的视图,根据Map中的数据映射关键字到指定的视图。绑定数据到视图分两个阶段,首先,如果设置了SimpleAdapter.ViewBinder,那么这个设置的ViewBinder的setViewValue(android.view.View, Object, String)将被调用。如果setViewValue的返回值是true,则表示绑定已经完成,将不再调用系统默认的绑定实现。如果返回值为false,视图将按以下顺序绑定数据:
如果View实现了Checkable(例如CheckBox),期望绑定值是一个布尔类型。
TextView.期望绑定值是一个字符串类型,通过调用setViewText(TextView, String)绑定。
ImageView,期望绑定值是一个资源id或者一个字符串,通过调用setViewImage(ImageView, int) 或 setViewImage(ImageView, String)绑定数据。
如果没有一个合适的绑定发生将会抛出IllegalStateException。

先看一下构造函数: 

复制代码 代码如下:

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

SimpleAdapter基本上认知了其参数含义 用起来就简单多了。

SimpleAdapter的参数说明:

  •  第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要
  •  第二个参数表示生成一个Map(String ,Object)列表选项
  •  第三个参数表示界面布局的id  表示该文件作为列表项的组件
  •  第四个参数表示该Map对象的哪些key对应value来生成列表项
  •  第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系
  •  注意的是map对象可以key可以找不到 但组件的必须要有资源填充  因为 找不到key也会返回null 其实就相当于给了一个null资源

 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head}

这个head的组件会被name资源覆盖

示例代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="horizontal" 
  tools:context=".MainActivity" > 
 
  <ListView 
    android:id="@+id/lt1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
  </ListView> 
 
</LinearLayout> 

<?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" > 
 
  <ImageView 
    android:id="@+id/head" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp" /> 
 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 
     
    <TextView  
      android:id="@+id/name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="20dp" 
      android:textColor="#f0f" 
      android:paddingLeft="10dp"/> 
     
         
    <TextView  
      android:id="@+id/desc" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="14dp" 
      android:paddingLeft="10dp"/> 
     
  </LinearLayout> 
 
</LinearLayout> 


package com.example.simpleadptertest; 
 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
 
public class MainActivity extends Activity { 
 
  private String[] name = { "剑萧舞蝶", "张三", "hello", "诗情画意" }; 
 
  private String[] desc = { "魔域玩家", "百家执行", "高级的富一代", "妹子请过来..一个善于跑妹子的。。" }; 
 
  private int[] imageids = { R.drawable.libai, R.drawable.nongyu, 
      R.drawable.qingzhao, R.drawable.tiger }; 
   
  private ListView lt1; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    List<Map<String, Object>> listems = new ArrayList<Map<String, Object>>(); 
    for (int i = 0; i < name.length; i++) { 
      Map<String, Object> listem = new HashMap<String, Object>(); 
      listem.put("head", imageids[i]); 
      listem.put("name", name[i]); 
      listem.put("desc", desc[i]); 
      listems.add(listem); 
    } 
     
    /*SimpleAdapter的参数说明 
     * 第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要 
     * 第二个参数表示生成一个Map(String ,Object)列表选项 
     * 第三个参数表示界面布局的id 表示该文件作为列表项的组件 
     * 第四个参数表示该Map对象的哪些key对应value来生成列表项 
     * 第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系 
     * 注意的是map对象可以key可以找不到 但组件的必须要有资源填充 因为 找不到key也会返回null 其实就相当于给了一个null资源 
     * 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head} 
     * 这个head的组件会被name资源覆盖 
     * */ 
    SimpleAdapter simplead = new SimpleAdapter(this, listems, 
        R.layout.simple_item, new String[] { "name", "head", "desc" }, 
        new int[] {R.id.name,R.id.head,R.id.desc}); 
     
    lt1=(ListView)findViewById(R.id.lt1); 
    lt1.setAdapter(simplead); 
     
  } 
 
  @Override 
  public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
  } 
 
} 

2016424102052967.jpg (418×350)

相关文章

  • Android开发笔记 改变字体颜色的三种方法

    Android开发笔记 改变字体颜色的三种方法

    在TextView中添加文本时有时需要改变一些文本字体的颜色,今天学到了三种方法,拿出来分享一下
    2012-11-11
  • Android布局之LinearLayout线性布局

    Android布局之LinearLayout线性布局

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布,下面通过本篇文章给大家介绍Android布局之LinearLayout线性布局,涉及到android linearlayout 布局相关知识,对本文感兴趣的朋友一起学习吧
    2015-12-12
  • android实现九宫格程序

    android实现九宫格程序

    这篇文章主要为大家详细介绍了android实现九宫格程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • Android仿QQ空间主页面的实现

    Android仿QQ空间主页面的实现

    今天模仿安卓QQ空间,打开程序的启动画面和导航页面我就不做了,大家可以模仿微信的那个做一下,很简单。这次主要做一下主页面的实现,感兴趣的朋友可以参考下
    2013-01-01
  • android文件存储和SharedPreferences存储的项目实例

    android文件存储和SharedPreferences存储的项目实例

    本文主要介绍了android文件存储和SharedPreferences存储的项目实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • Android 实现抖音小游戏潜艇大挑战的思路详解

    Android 实现抖音小游戏潜艇大挑战的思路详解

    《潜水艇大挑战》是抖音上的一款小游戏,最近特别火爆,很多小伙伴都玩过。接下来通过本文给大家分享Android 手撸抖音小游戏潜艇大挑战的思路,需要的朋友可以参考下
    2020-04-04
  • Android编程调用红外线遥控功能示例

    Android编程调用红外线遥控功能示例

    这篇文章主要介绍了Android编程调用红外线遥控功能,结合实例形式分析了Android红外线功能的相关组件调用与布局操作实现技巧,需要的朋友可以参考下
    2017-08-08
  • Android实现笑脸进度加载动画

    Android实现笑脸进度加载动画

    这篇文章主要介绍了Android实现笑脸进度加载动画的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下
    2021-05-05
  • Android自定义View实现微信支付密码输入框

    Android自定义View实现微信支付密码输入框

    这篇文章主要为大家详细介绍了Android自定义View实现微信支付密码输入框,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • Android SurfaceView画板操作

    Android SurfaceView画板操作

    这篇文章主要为大家详细介绍了Android SurfaceView画板操作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-05-05

最新评论