关于android studio的simpleAdapter的简单实现

先来效果图
效果图实现这个效果,需要两个xml文件和一个activity文件
第一个activity_list.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="horizontal"
    tools:context=".ListActivity">

    <ImageView
        android:id="@+id/imgtou"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:baselineAlignBottom="true"
        android:paddingLeft="8dp"/>
    <!--基线对齐底部-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="8dp"
            android:textColor="#1D1D1C"
            android:textSize="20sp"/>

        <TextView
            android:id="@+id/says"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="8dp"
            android:textColor="#B4B4B9"
            android:textSize="14sp"/>

    </LinearLayout>


</LinearLayout>

?第二个activity.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">

    <ListView
        android:id="@+id/list2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ListView>


</LinearLayout>

?接下来是activity.文件

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

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

public class MainActivity extends AppCompatActivity {
    //用三个数组装载数据

private String[] names= new String[]{"悟空","叶子","唐僧"};
private String[] says= new String[]{"是魔是佛我自己说了算","叶子的凋零树会心疼吗","对敌友善对友刁"};
private int[] imgIds=new int[]{R.mipmap.gongren,R.mipmap.gaodainshi,R.mipmap.jingcha};//三张图片地址


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       List<Map<String,Object>> listitem=new ArrayList<Map<String, Object>>();
       for (int i=0;i<names.length;i++){
           Map<String,Object> showitem=new HashMap<String, Object>();
           showitem.put("touxiang",imgIds[i]);
           showitem.put("name",names[i]);
           showitem.put("says",says[i]);
           listitem.add(showitem);
       }
       //创建一个simpleadapter

        SimpleAdapter myAdapter=new SimpleAdapter(
                getApplicationContext(),
                listitem,
                R.layout.activity_list,
                new  String[]{"touxiang","name","says"},
                new int[]{R.id.imgtou,R.id.name,R.id.says}
                );
       ListView listView=(ListView)findViewById(R.id.list2);
       listView.setAdapter(myAdapter);
    }
}

?注意点:
图片地址按自己的地址改,在引用两个xml文件时,不用引用错了,ok,搞定,我是小白,只是记录一下自己的学习感想,有不对的地方请大家指出,万分感谢。

发布了5 篇原创文章 · 获赞 0 · 访问量 224

猜你喜欢

转载自blog.csdn.net/xyj_1_2/article/details/101294760