当前位置: 移动技术网 > 移动技术>移动开发>Android > 举例讲解Android应用中SimpleAdapter简单适配器的使用

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

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)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网