zoukankan      html  css  js  c++  java
  • 安卓开发之SimpleAdapter的使用

    package com.lidaochen.test;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // 找到ListView控件
            ListView listView = (ListView)findViewById(R.id.lv);
            // 准备ListView要显示的数据
            List<Map<String, String>> data = new ArrayList<Map<String, String >>();
            Map<String, String> map1 = new HashMap<String, String>();
            map1.put("name", "张飞");
            map1.put("phone", "13688888888");
            Map<String, String> map2 = new HashMap<String, String>();
            map2.put("name", "马云");
            map2.put("phone", "13777777777");
            Map<String, String> map3 = new HashMap<String, String>();
            map3.put("name", "刘强东");
            map3.put("phone", "13222222222");
            Map<String, String> map4 = new HashMap<String, String>();
            map4.put("name", "马化腾");
            map4.put("phone", "13666666666");
            // 将map加入到集合中
            data.add(map1);
            data.add(map2);
            data.add(map3);
            data.add(map4);
            // 创建一个SimpleAdapter
            SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.item,
                    new String[]{"name", "phone"}, new int[]{R.id.tv_name, R.id.tv_phone});
            // 设置适配器
            listView.setAdapter(simpleAdapter);
        }
    }
    <?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">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/tv_name"
            android:textSize="20sp"
            android:layout_weight="1"
            android:textColor="#d20606"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:id="@+id/tv_phone"
            android:layout_weight="1"/>
    
    
    </LinearLayout>
  • 相关阅读:
    一步一步学习开发BPM工作流系统(三)开发WinForm的应用平台1
    支持多数据库本地和远程调用的数据访问层架构
    HF Web表单开发技术文档
    CDZSC_2015寒假新人(2) 数学 C
    CDZSC_2015寒假新人(2)——数学 A
    ZSC 1306: 沼跃鱼早已看穿了一切 题解
    解决”java.lang.UnsatisfiedLinkError: Native Library .dll already loaded in another classloader”的问题
    有目标就要坚持
    (转)新兴XML处理方法VTDXML介绍
    (转)Java远程通讯可选技术及原理
  • 原文地址:https://www.cnblogs.com/duxie/p/10912077.html
Copyright © 2011-2022 走看看