android适配器:SimpleAdapter

1.首先写一个Adapter项布局文件,主要用来设置listView每一项的布局
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.myapplication.ToastActivity"
    android:orientation="horizontal"
    >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:text="姓名:"
    />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text1"
        android:text="内容"
        android:gravity="center"
        android:layout_weight="0.3"

        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="年龄:"
        />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text2"
        android:text="内容"
        android:gravity="center"
        android:layout_weight="0.3"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="性别:"
        />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text3"
        android:text="内容"
        android:gravity="center"

        android:layout_weight="0.3"
        />

</LinearLayout>

效果:



  1. 2.编写我们的java代码
    List<Map<String,String>> listMap=new ArrayList<Map<String,String>>();//数据源
    Map<String,String> map1=new HashMap<String,String>();
            map1.put("name","张三");
            map1.put("age","20");
            map1.put("sex", "男");
        Map<String,String> map2=new HashMap<String,String>();
            map2.put("name","小红");
            map2.put("age", "20");
            map2.put("sex", "女");
        listMap.add(map1);
        listMap.add(map2);

            SimpleAdapter simpleAdapter=new SimpleAdapter
                    (
                            this,//上下文
                            listMap,//数据源
                            R.layout.simple_adapter_item,//指向自定义的项布局文件
                            new String[]{"name","age","sex"},//key数组
                            new int[]{R.id.text1,R.id.text2,R.id.text3}//项布局文件里的控件数组
                            /**
                             * 简单理解为:从list<map>中获取每一个map元素,根据key获取内容放到指定的项布局文件中的指定控件,最后形成一项
                             * 后俩个数组参数的元素是是对应的
                             */
                    );
            listView.setAdapter(simpleAdapter);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值