使用SimpleAdapter创建ListView

作者: 你好_摆渡人 | 来源:发表于2019-10-11 17:13 被阅读0次

ArrayAdapter的功能比较有限,他的列表项只能是TextView,如果开发实现需要更加复杂的列表项,则可以考虑SimpleAdapter
首先定义一个ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal">
 <ListView
     android:id="@+id/mylist"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     />

</LinearLayout>

该listView将显示simpleAdapter提供的列表项
下面是Activity代码:

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
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[] descs=new  String[]{"可爱的小孩","一个擅长音乐的女孩","一个擅长文学的女性","一个小鲁班的巴巴"};
    private int [] imageIds=new int[]{R.drawable.tx,R.drawable.tx,R.drawable.tx,R.drawable.tx};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建一个list集合,list集合的元素是map
        List<Map<String,Object>> listItems=new ArrayList<>();
        for (int i=0;i<names.length;i++)
        {
            Map<String,Object> listItem=new HashMap<>();
            listItem.put("header",imageIds[i]);
            listItem.put("personName",names[i]);
            listItem.put("desc",descs[i]);
            listItems.add(listItem);
        }
     //创建一个SimpleAdapter
        SimpleAdapter simpleAdapter =new SimpleAdapter(this,listItems,R.layout.simple_item,new String[]{"personName","header","desc"},new int[]{R.id.name,R.id.header,R.id.desc});
        ListView list=findViewById(R.id.mylist);
        //为ListView设置Adapter
        list.setAdapter(simpleAdapter);
    }
}

simpleAdapter需要个参数
第一个this
第二个参数:该参数应该是一个List<?extends Map<String,?>>类型的集合对象,该集合中每个Map<String,?>对象生成一个列表项。
第三个参数:该参数是个布局id。该。xml文件作为列表项组件。
第四个参数:该参数是个String【】类型的参数,该参数取决定提取Map<String,?>对象中那些key对应的value来生成列表项
第五个参数:该参数应该是一个int【】类型的参数,该参数决定补充那些组件
从上面的程序看。listItems是一个长度为4的集合,这就意味着它生成的ListView将会包含4个列表项,每个列表项都是R.layout.simple_item对应的组件(也就是一个LinearLayout组件)。LinearLayout中包含了3个组件,这些组件内容由listItems集合提供。
R.layout.simple.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal">
    //定义一个ImageView,用于作为列表项的一部分
    <ImageView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"/>
    <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="10dp"
            android:textColor="#f0f"
            android:textSize="20dp"/>
        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:textSize="14dp"/>
    </LinearLayout>
</LinearLayout>

效果图:


image.png

知识拓展:
设置监听用户点击,通过AdapterView的setOnItemClickListener()方法
在activity中加入如下代码监听

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("111",names[position]+"被选中了");
            }
image.png

这里小编遇到一个报错就是log提示不存在
其实Log已经报红了,蛮尝试下运行(快捷键shift+F10)下,就会看到报错信息,其实很简单的问题,就是忘记导包了!

添加:

import android.util.Log;

相关文章

网友评论

    本文标题:使用SimpleAdapter创建ListView

    本文链接:https://www.haomeiwen.com/subject/vahxmctx.html