原创 Android之使用 SimpleAdapter 实现 ListView

2021-5-26 13:00 1850 15 2 分类: 软件与OS 文集: android studio

使用 SimpleAdapter来实现ListView有一定的扩充性,可以实现一定的自定义效果,这种自定义的效果是通过创建 item 样式来实现的。创建一个名为 item_list.xmlitem布局文件,代码如下

<?xml version="1.0" encoding="utf-8"?>
  • <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  • xmlns:tools="http://schemas.android.com/tools"
  • android:layout_width="match_parent"
  • android:layout_height="match_parent"
  • android:orientation="horizontal"
  • tools:context=".MainActivity">
  • <ImageView
  • android:id="@+id/item_image"
  • android:layout_width="0dp"
  • android:layout_height="wrap_content"
  • android:layout_weight="1"/>
  • <TextView
  • android:id="@+id/item_text"
  • android:layout_width="0dp"
  • android:layout_height="wrap_content"
  • android:layout_weight="1
  • android:textSize="24dp"/>
  • </LinearLayout>
  • 复制代码

    MainActivity的布局文件Activity_main.xml中加入一个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"
  • android:layout_width="match_parent"
  • android:layout_height="match_parent"
  • android:orientation="horizontal"
  • tools:context=".MainActivity">
  • <ListView
  • android:id="@+id/listView"
  • android:layout_width="match_parent"
  • android:layout_height="wrap_content"/>
  • </LinearLayout>
  • 复制代码

    MainActivity中我们需要使用SimpleAdapter适配器来实现ListView,代码如下:

    package com.rfstar.listviewtest02;
  • 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 MainActivity extends AppCompatActivity {
  • private ListView listView;
  • private List<Map<String,Object>> dataList=new ArrayList<Map<String, Object>>();
  • private int[] itemIdArray=new int[]{R.id.item_text,R.id.item_image};
  • private String[] dataKeyArray=new String[]{"name","image"};
  • @Override
  • protected void onCreate(Bundle savedInstanceState) {
  • super.onCreate(savedInstanceState);
  • setContentView(R.layout.activity_main);
  • initData();
  • initView();
  • }
  • private void initView() {
  • listView=(ListView)findViewById(R.id.listView);
  • SimpleAdapter simpleAdapter=new SimpleAdapter(this,dataList,R.layout.item_list,dataKeyArray,itemIdArray);
  • listView.setAdapter(simpleAdapter);
  • }
  • private void initData() {
  • Map<String,Object>map;
  • for(int i=0;i<10;i++)
  • {
  • map=new HashMap<String,Object>();
  • map.put("name","大鸟科创空间"+i);
  • map.put("image",R.mipmap.ic_launcher);
  • dataList.add(map);
  • }
  • }
  • }
  • 复制代码

    本实例代码和使用 ArrayAdapter实现列表的代码大同小异,区别主要在于数据集的不同,以及实例化 Adapter 时的不同。SimpleAdapter 在实例化时需要传入5个参数,分别是上下文对象、List,Object>>格式的数据集、item的布局对象、数据集中Map键的数组item 中控件的id数组。当 SimpleAdapter 实例化完成后,系统会根据传入的两个数组自动将数据集注入 item 中。使用 SimpleAdapter 适配器实现的 ListView 能够实现更加复杂的状况,在实际开发中有时会使用到。但是,它有一个非常大的缺陷。想象一下,如果在每个item 中都有一个按钮,需要给这个按钮添加点击事件,这就变得无法处理了。此时只能依靠使用 BaseAdapter 实现的 ListView下一章节我会给大家将BaseAdapter的使用。运行程序效果如下图:

    android studio工具及手机模拟器以及更多工程源代码下载请前往微信公众号:大鸟科创空间,回复:android studio即可获取。


    作者: 大鸟科创空间, 来源:面包板社区

    链接: https://mbb.eet-china.com/blog/uid-me-3949041.html

    版权声明:本文为博主原创,未经本人允许,禁止转载!

    PARTNER CONTENT

    文章评论1条评论)

    登录后参与讨论

    curton 2021-5-26 19:46

    学习了
    相关推荐阅读
    大鸟科创空间 2022-02-14 17:42
    生日快乐HTML浪漫网页制作源码
    对象生日快到了,赶快下载去给ta一个惊喜吧。解压后里面有整个项目和使用说明。这个代码有背景音乐(可以自己更改),有自转相册(可以改成对象照片)如下是效果截图,用鼠标滑动界面会有爱心划过,下滑可以循环播...
    大鸟科创空间 2022-01-18 18:06
    Android之WebView用法
    除了HTTP通信与 Socket 通信两种主要的网络技术外,在 Android 中还提供了一种加载和显示网页的技术—WebView。这可以让我们去处理一些特殊的需求,比如像微信那样在应用程序里展示网页...
    大鸟科创空间 2022-01-07 14:18
    Android之Socket实例
        Socket(套接字)是对 TCP/IP 协议的封装和应用,根据底层封装协议的不同,Socket 的类型可以分为流套接字(streamsocket)和数据报套接字(data...
    大鸟科创空间 2021-12-24 14:38
    Android之本地广播的使用
    前面我们发送和接收的广播全部属于系统全局广播,即发出的广播可以被其他任何应用程序接收到,并且我们也可以接收来自于其他任何应用程序的广播。这样就很容易会引起安全性的问题,比如说我们发送的一些携带关键性数...
    大鸟科创空间 2021-12-15 12:50
    自定义广播实例
    我们应该已经学会了通过广播接收者来接收系统广播的内容,但是在实际开发中,仍需要自定义一些广播。下面我们就来讲解如何在应用程序中发送自定义的广播。发送广播很简单,只需要声明一个意图,然后使用Contex...
    大鸟科创空间 2021-12-07 13:04
    Android之动态注册广播实例
    Android内置了很多系统级别的广播,我们可以在应用序中通过监听这些广播来得到各种系统的状态信息,比如手机开机完成后会发出一条广播、电池的电最发生变化会发出一条广播、时间或时区发生改变也会发出一条广...
    我要评论
    1
    15
    关闭 站长推荐上一条 /3 下一条