zoukankan      html  css  js  c++  java
  • simpleAdapter

    这个adapter用起来稍微复杂一点。

    首先我们看main_activity的布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!-- 定义一个ListView -->
        <ListView android:id="@+id/mylist"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    在界面上只有一个listView

    然后,我们在主函数里面利用simpleAdapter向这个listView填充内容

    package com.example.adapter
    
    import android.support.v7.app.AppCompatActivity
    import android.os.Bundle
    import android.util.Log
    import android.view.View
    import android.widget.AdapterView
    import android.widget.ArrayAdapter
    import android.widget.ListView
    import android.widget.SimpleAdapter
    
    class MainActivity : AppCompatActivity() {
    
        private val names = arrayOf("虎头", "弄玉", "李清照", "李白")
        private val descs = arrayOf("可爱的小孩", "一个擅长音乐的女孩", "一个擅长文学的女性", "浪漫主义诗人")
        private val imageIds = intArrayOf(R.drawable.tiger,
            R.drawable.nongyu, R.drawable.qingzhao, R.drawable.libai)
        override fun onCreate(savedInstanceState: Bundle?)
        {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            // 创建一个List集合,List集合的元素是Map
            val listItems = ArrayList<Map<String, Any>>()
            for (i in names.indices)
            {
                val listItem = HashMap<String, Any>()
                listItem["header"] = imageIds[i]
                listItem["personName"] = names[i]
                listItem["desc"] = descs[i]
                listItems.add(listItem)
            }
            // 创建一个SimpleAdapter
            val simpleAdapter = SimpleAdapter(this, listItems, R.layout.simple_item, arrayOf("personName", "header", "desc"), intArrayOf(R.id.name, R.id.header, R.id.desc))
            val list = findViewById<ListView>(R.id.mylist)
            // 为ListView设置Adapter
            list.adapter = simpleAdapter
            // 为ListView的列表项的单击事件绑定事件监听器
            list.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
                Log.i("-CRAZYIT-", names[position] + "被单击了")
            }
            // 为ListView的列表项的选中事件绑定事件监听器
            list.onItemSelectedListener = object : AdapterView.OnItemSelectedListener
            {
                // 第position项被选中时激发该方法
                override fun onItemSelected(parent: AdapterView<*>, view: View,
                                            position: Int, id: Long)
                {
                    Log.i("-CRAZYIT-", names[position] + "被选中了")
                }
    
                override fun onNothingSelected(parent: AdapterView<*>)
                {
                }
            }
        }
    }
    View Code

    主要看simpleAdapter的使用

     val simpleAdapter = SimpleAdapter(this, listItems, R.layout.simple_item, arrayOf("personName", "header", "desc"), intArrayOf(R.id.name, R.id.header, R.id.desc))

    这里传入5个参数:

    • context
    • List<?extends Map<String,?>>
    • 指定界面布局的ID
    • string[] 指定从第二个参数中提取哪些值,填充到布局界面中
    • 指定填充的对应关系

     最后我们看一下指定的布局文件 simple_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!-- 定义一个ImageView,用于作为列表项的一部分。 -->
        <ImageView
            android:id="@+id/header"
            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,用于作为列表项的一部分。 -->
            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:textColor="#f0f"
                android:textSize="20dp" />
            <!-- 定义一个TextView,用于作为列表项的一部分。 -->
            <TextView
                android:id="@+id/desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:textSize="14dp" />
        </LinearLayout>
    </LinearLayout>
  • 相关阅读:
    IDEA 运行junit单元测试方法
    IDEA 修改编码
    接口文档word版
    java 上传文件到七牛云中
    单例模式
    洛谷P3092 [USACO13NOV]没有找零No Change
    Codevs 1159 最大全0子矩阵
    洛谷P2733 家的范围 Home on the Range
    洛谷P2280 [HNOI2003]激光炸弹
    洛谷P2023 [AHOI2009]维护序列
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/11481223.html
Copyright © 2011-2022 走看看