AndroidStudio中ListView的使用方法之SimpleAdapter适配器

Posted 想飞--++

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AndroidStudio中ListView的使用方法之SimpleAdapter适配器相关的知识,希望对你有一定的参考价值。

androidStudio中ListView的四种使用方法之第三种SimpleAdapter适配器。

首先我们先了解一下SimpleAdapter类。

SimpleAdapter类是用来处理ListView显示数据的,这个类可以将任何自定义的xml布局文件作为列表项来使用。

SimpleAdapter构造方法的原型为:

public SimpleAdapter ( Context context,List<?extends Map<String,?>> data,int resource,String[] form,int[] to)
参数值关键字或名称的作用
contextSimpleAdapter关联的View的运行环境
data一个Map组成的List。列表种的每个条目对应列表中的一行,每个Map中应该包含from参数中指定的键
resource定义列表项的布局文件的资源id
from被添加到Map映射上的键名
to将绑定数据的视图的Id和from参数对应

 

以上是SimpleAdapter的基础知识,接下来我们用一个例子来展示SimpleAdapter的具体使用方法。

创建一个名为ListViewSimpleAdapter的空白Activity,并为其xml文件添加一个ListView组件。

在ListViewSimpleAdapter.java文件中连接xml文件和ListView主键,并创建所需要的数据。

接下来我们要创建一个SimpleAdapter适配器,由SimpleAdapter的基础可知我们需要一个列表项的布局资源。

由此我们新建一个layoutitem1.xml文件,作为一个列表项的资源文件,并在这个xml文件布局。如图所示

准备好以后我们再次回到ListViewSimpleAdapter.java中创建SimpleAdapter适配器,并为适配器添加好对应的参数。

看到getData()报错没关系,我们按alt+enter键创建一个getData()方法用来添加数据,并为listview添加SimpAdapter适配器。

最后在AndroidMainfest.xml中为MainActivity2添加过滤器,运行就行了。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ListViewSimpleAdapter">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  </application>

</manifest>

效果图如下:

 

原代码如下:

ListViewSimpleAdapter.java

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
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 ListViewSimpleAdapter extends AppCompatActivity 
    ListView listView;
    int[] imageArr=R.drawable.diqiu,R.drawable.jinxing,R.drawable.muxing,R.drawable.tuxing,R.drawable.huoxing;
    String[] titleArr="地球","金星","木星","土星","火星";
    String[] contentArr="地球的介绍","金星的介绍","木星的介绍","土星的介绍","火星的介绍";
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_simple_adapter);
        //连接listView
        listView=(ListView)findViewById(R.id.ListViewsimple);
        //创建SimpleAdapter适配器
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,getData(),R.layout.layoutitem1,
               new String[]"img","name","content",new int[]R.id.image2,R.id.textTitle,R.id.textContent);
        //为listview添加SimpleAdapter适配器
        listView.setAdapter(simpleAdapter);
    

    private List<? extends Map<String,?>> getData() 
        List<Map<String,Object>> list;
        Map<String,Object> map;
        list=new ArrayList<Map<String,Object>>();

        for(int i=0;i<imageArr.length;i++)
            map=new HashMap<String,Object>();
            map.put("img",imageArr[i]);
            map.put("name",titleArr[i]);
            map.put("content",contentArr[i]);
            list.add(map);
        

        return list;
    

activity_list_view_simple_adapter.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"
    tools:context=".ListViewSimpleAdapter">


    <ListView
        android:id="@+id/ListViewsimple"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

layoutitem1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
<ImageView
    android:id="@+id/image2"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/img8"></ImageView>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="地球"
            android:textStyle="bold"
            android:layout_marginTop="20dp"
            android:textSize="18sp">
        </TextView>
        <TextView
            android:id="@+id/textContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="地球的介绍"
            android:layout_marginTop="20dp"></TextView>
    </LinearLayout>
</LinearLayout>

我们还可以使用setOnItemClickListener方法来对每一个列表项添加点击事件,在这里我就不详细讲解了

我将在下一篇博客中展示自定义适配器的使用方法

小白在线欢迎指点。

以上是关于AndroidStudio中ListView的使用方法之SimpleAdapter适配器的主要内容,如果未能解决你的问题,请参考以下文章

AndroidStudio中ListView的使用方法之SimpleAdapter适配器

AndroidStudio中ListView的使用方法之SimpleAdapter适配器

AndroidStudio中ListView的使用方法之SimpleAdapter适配器

AndroidStudio_ListView

Listview和复选框问题Android Studio

Android Studio,如何使用数组或光标中的数据填充自定义 ListView?