[Android development] SimpleAdapter

SimpleAdapter main container

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SimpleActivity">

    <ListView
        android:id="@+id/list_view2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</androidx.constraintlayout.widget.ConstraintLayout>

Item layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/qq_img"
        android:layout_width="50dp"
        android:layout_height="65dp"
        android:background="@mipmap/caocao"/>

    <TextView
        android:id="@+id/qq_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/qq_img"
        android:layout_marginLeft="5dp"
        android:textSize="26sp"
        android:text="Caocao" />

    <TextView
        android:id="@+id/qq_mood"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/qq_img"
        android:layout_below="@id/qq_name"
        android:layout_marginLeft="5dp"
        android:textColor="#2ae0c8"
        android:text="xixixi"/>

</RelativeLayout>

Configure SimpleAdapter

package com.example.fragmentapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.AdapterView
import android.widget.ListView
import android.widget.SimpleAdapter
import android.widget.Toast
import java.util.ArrayList
import java.util.HashMap

class SimpleActivity : AppCompatActivity() {
    
    
    private val data: MutableList<Map<String, Any>> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
    
    
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_simple)

        //获取ListView对象
        val listView2 = findViewById<ListView>(R.id.list_view2)

        //实例化适配器对象
        //参数1:环境上下文(this)
        //参数2:数据源
        initData()
        //参数3:每一项布局
        //参数4:数据来源的key数组
        val from = arrayOf<String?>("img", "name", "mood")
        //参数5:数据去向的id数组
        val to = intArrayOf(R.id.qq_img, R.id.qq_name, R.id.qq_mood)
        //参数45的对应索引上,from数组的元素代码数据源
        //每个map的key所只带的数据会作为to数组对应索引上id所代表的控件的内容显示处理
        val adapter = SimpleAdapter(this, data, R.layout.item3, from, to)

        //为ListView设置适配器
        listView2.adapter = adapter

        //点击事件
        listView2.setOnItemClickListener {
    
     adapterView, view, i, l ->
            //用Toast提示Name  Mood
            //i表示要检索的元素的索引
            val map: Map<String, Any>? = data[i]
            val name = map!!["name"].toString()
            val mood = map["mood"].toString()
            Toast.makeText(this@SimpleActivity, "$name   $mood", Toast.LENGTH_SHORT).show()
        }
    }

    private fun initData() {
    
    
        //左边:头像   右上:名字   右下:心情
        val map1: MutableMap<String, Any> = HashMap()
        map1["img"] = R.mipmap.caocao
        map1["name"] = "曹操"
        map1["mood"] = "宁教我负天下人,休教天下人负我"
        val map2: MutableMap<String, Any> = HashMap()
        map2["img"] = R.mipmap.zhenji
        map2["name"] = "甄姬"
        map2["mood"] = "飘摇兮若流风之回雪,仿佛兮若轻云之蔽月"

        data.add(map1)
        data.add(map2)
    }
}

Guess you like

Origin blog.csdn.net/weixin_42020386/article/details/112916970