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

示例图:

Android : ListView + SimpleAdapter(简单适配器)-简单应用_第1张图片

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> data
        List< Map> datas = new ArrayList<>();
        for(int i =0 ; i 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> 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




    
    

内容 content_layout.xml




    

    
    

    
    
    
    
    
    

你可能感兴趣的:(Android,相关,android)