示例图:
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> 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