Android : ListView + SimpleAdapter(简单适配器)-简单应用

示例图:

MainActivity.java

package com.example.mylistviewsimpleadapter;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//简单的适配器 应用
public class MainActivity extends AppCompatActivity {

    //数据源
    //图片
    private int[] arrImg = new int[]{
            R.mipmap.a, R.mipmap.b, R.mipmap.c,
            R.mipmap.d, R.mipmap.e, R.mipmap.f,
            R.mipmap.g, R.mipmap.h, R.mipmap.i,
            R.mipmap.a, R.mipmap.b, R.mipmap.c,
            R.mipmap.a, R.mipmap.b, R.mipmap.c
    };
    //标题
    private String[] arrTitle = {
            "张三","李四","张三丰",
            "小学群聊","周芷若","周先生",
            "老领导","玖龙玺","阿道夫",
            "张三","李四","张三丰",
            "张三","李四","张三丰"
    };

    //内容
    private String[] arrContent =new String[]{
            "早啊!","你在干啥","你最近在练什么武功",
            "呼叫XXX家长","最近咋样","吃饭了吗?",
            "啥时候有空喝茶呀?",".....","小伙子,学魔法吗?",
            "早啊!","你在干啥","你最近在练什么武功",
            "早啊!","你在干啥","你最近在练什么武功"
    };

    //时间
    private String[] arrTime = {
            "刚刚","昨天","11-20",
            "10-1","2023-11-20","17:30",
            "9:00","10-1","2000-01-01",
            "刚刚","昨天","11-20",
            "刚刚","昨天","11-20"
    };

    private SimpleAdapter SimpleAdapter =null;
    private Context context;
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.btn_list_view);

        context = this;
        //设置数据源  List<? extends Map<String, ?>> data
        List< Map<String, Object>> datas = new ArrayList<>();
        for(int i =0 ; i<arrImg.length;i++){
            //各项数据
            Map<String,Object> itemData = new HashMap<>();

            itemData.put("item_img",arrImg[i]);
            itemData.put("item_title",arrTitle[i]);
            itemData.put("item_content",arrContent[i]);
            itemData.put("item_time",arrTime[i]);

            //添加到List
            datas.add(itemData);
        }

//    第1个参数: Context
//    第2个参数: 数据 List<map<String,Object>> datas
//    第3个参数: xml资源文件 R.layout....
//    第4个参数: 控制从datas中取出哪些数据 与map中的 key一致
//    第5个参数: 布局文件的id  控制取出的数据要填充哪些界面元素。
        SimpleAdapter = new SimpleAdapter(context,datas,R.layout.content_layout,
                new String[]{"item_img","item_title","item_content","item_time"},
                new int[]{R.id.btn_image_view,R.id.btn_title_view,R.id.btn_tv_content,R.id.btn_time});

        //往容器中设置 适配器
        listView.setAdapter(SimpleAdapter);

        //事件机制
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,"您点击了"+position+"获取内容"+arrContent[position],Toast.LENGTH_SHORT).show();
            }
        });
    }
}

主布局文件 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="**通讯信息**"
        android:textSize="40dp"
        android:textStyle="bold"
        />
    <ListView
        android:id="@+id/btn_list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        />

</LinearLayout>

内容 content_layout.xml

<?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">
<!-- RelativeLayout 相对布局-->
    <ImageView
        android:id="@+id/btn_image_view"
        android:layout_width="50dp"
        android:layout_height="50dp"

        android:layout_marginLeft="10dp"
        />

    <!-- 标题
    在图片的右边
    -->
    <TextView
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/btn_image_view"
        android:id="@+id/btn_title_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"

        android:textStyle="bold"
        />

    <!-- 画条线
    在图片的右边
    在文字的下面
    -->
    <TextView
        android:id="@+id/btn_tv_line"
        android:layout_below="@+id/btn_title_view"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/btn_image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ccc"
        android:minHeight="1dp"
        android:maxHeight="1dp"
        />
    <!-- 内容
    在图片的右边
    在线的下面
    -->
    <TextView
        android:layout_below="@+id/btn_tv_line"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/btn_image_view"
        android:id="@+id/btn_tv_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"

        />
    <!-- 时间提示
    在最右边显示
    在父元素内右边
    -->
    <TextView
        android:id="@+id/btn_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_alignParentRight="true"
        />

</RelativeLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

javaGHui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值