Android——列表视图(ListView)

article/2025/9/26 11:01:49

1、什么是ListView?它可以实现怎样的功能?

列表视图是android中最常用的一种视图组件,它以垂直列表的形式列出需要显示的列表项。手机屏幕空间有限,能显示的内容不多。可以借助ListView来显示更多、更丰富的内容。ListView允许用户通过上下滑动来将屏幕外的数据滚动到屏幕内,同时屏幕内原有的数据滚动出屏幕,从而显示更多的数据内容。列表视图如下图所示。


2、由于列表视图中的每一个列表项的格式都是一样的,所以我们只需要定义出一个列表项的格式(框架),再把数据逐个放入这样的框架中即可。

(1)那么如何定义出这种框架呢?

在Android中提供了两种适配器用于实现列表项格式的设置。

①ArrayAdapter

这是一种简单易用的Adapter,通常用于将数组或List集合作为列表项数据源

②SimpleAdapter

SimpleAdapter(Context context, List<? extends Map<String, ?>> data,      int resource,    String[] from,    int[] to) 

它共有5个参数:

Context context:-------------------------------------

List<? extends Map<String, ?>> data:--------- 数据源

int resource----------------------------------------------布局文件

String[] from----------------------------------------------数据源中键值对数据中的键

int[] to------------------------------------------------------布局文件中控件对象名


3、实例

(1)首先在布局文件activity_main.xml里面插入ListView组件

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.listview.MainActivity" ><ListView android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/listview"></ListView>
</RelativeLayout>

(2)建立列表项框架showfruit.xml布局文件

我们分析一下单个的列表项可以把它看成是从左至右的水平线性布局,第一个为图片,

考虑使用ImageView组件,第二个再次看成一个垂直的线性布局(分别是水果名称和水果产地)。这里用到了LinearLayout的嵌套。代码如下:

<?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" ><ImageViewandroid:id="@+id/fruitPic"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextView android:id="@+id/fruitName"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="20dp"android:text="苹果"/><TextView android:id="@+id/fruitHome"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="新疆"/></LinearLayout>
</LinearLayout>

 

(3)最后我们编写MainActivity.java文件

package com.example.listviewproject;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;public class MainActivity extends Activity {ListView list;Map<String,Object> fruit1,fruit2,fruit3;List<Map<String,Object>> fruits;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);list=(ListView)findViewById(R.id.listview);fruits=new ArrayList<Map<String,Object>>();fruit1=new HashMap();fruit1.put("fruitpic", R.drawable.ic_launcher);fruit1.put("fruitname", "苹果");fruit1.put("fruithome", "新疆");fruits.add(fruit1);fruit2=new HashMap();fruit2.put("fruitpic", R.drawable.ic_launcher);fruit2.put("fruitname", "香蕉");fruit2.put("fruithome", "河南");fruits.add(fruit2);fruit3=new HashMap();fruit3.put("fruitpic", R.drawable.ic_launcher);fruit3.put("fruitname", "柠檬");fruit3.put("fruithome", "广西");fruits.add(fruit3);String []from={"fruitpic","fruitname","fruithome"};int []to={R.id.fruitPic,R.id.fruitName,R.id.fruitHome};SimpleAdapter adapter=new SimpleAdapter(MainActivity.this,fruits,R.layout.showfruit, from, to);list.setAdapter(adapter);}
}

 


http://chatgpt.dhexx.cn/article/b1w0c321.shtml

相关文章

ListView详细介绍与使用

前言介绍&#xff1a; 关于 ListView 我们大家都应该是非常的熟悉了&#xff0c;在 Android 开发中是经常用到的&#xff0c;今天就再来回顾一下&#xff0c;ListView 的使用方法&#xff0c;和一些需要优化注意的地方&#xff0c;还有日常开发过程中的一些小技巧和经验。 Li…

Android最常用的控件ListView(详解)

一.ListView简介 在Android开发中&#xff0c;ListView是一个比较常用的控件。它以列表的形式 展示具体数据内容&#xff0c;并且能够根据数据的长度自适应屏幕显示。 二.ListView简单用法 代码部分 1.布局界面 activity_main.xml 代码&#xff1a; <?xml version"1…

ListView的用法

一、 ListView的使用 <ListView>:用于展示大量数据的一种列表视图,通过上下滑动的方式将屏幕外的数据滚动到屏幕内。 数据无法直接传递给ListView,需要适配器 Adapter:作用是将各种数据以合适的形式展示到View上 实例&#xff1a; Food.java: public class Food {priv…

Android原生AlertDialog修改标题,内容,按钮颜色,字体大小等

private void showAlerDialog() {AlertDialog dialog new AlertDialog.Builder(this).setTitle("AlerDialog").setMessage("这是一个AlertDialog").setPositiveButton("确定",null).setNegativeButton("取消",null).create();dialog.…

【android学习】Dialog对话框

1&#xff0c;Dialog 1&#xff09;onCreateDialog(int) 2&#xff09;showDialog(int) 第一次请求时&#xff0c;会从Activity中调用onCreateDialog。 3&#xff09;onPrepareDialog(int,Dialog) 在每次打开对话框时被调用。 4&#xff09;dismissDialog(int) 关闭对话…

Android Dialog使用详解

对话框是提示用户作出决定或输入额外信息的小窗口&#xff0c;通常不会填充整个屏幕&#xff0c;用于进行一些额外交互 Dialog 类是对话框的基类&#xff0c;但应该避免直接实例化 Dialog&#xff0c;而应使用其子类&#xff0c;比如 AlertDialog 此对话框可显示标题、提示信…

Android 修改AlertDialog原生setPositiveButton的字体颜色背景颜色大小边距位置

看效果图&#xff1a; public void lanyaClick(View v) {//点击确定之后转向登陆框LayoutInflater factory LayoutInflater.from(Beforestart.this);//得到自定义对话框final View DialogView factory.inflate(R.layout.item_alert_dialog, null);//创建对话框android.app.Al…

setPositiveButton和setNegativeButton的区别

setPositiveButton和setNegativeButton的区别和setNeutralButton的区别 三者都是AlertDialog弹出框的按钮&#xff0c;都是封装好的button&#xff0c;只是显示的位置不同&#xff0c;项目中可根据情况选择使用&#xff0c;setNegativeButton一般用于确认&#xff0c;setNegat…

GPS(rinex格式)数据解析详细解读

RINEX格式现如今已成为GPS测量应用中的标准数据格式&#xff0c;目前应用最为广泛、最普遍的是RINEX格式的第2个版本&#xff0c;该版本能够用于包括静态和动态GPS测量在内的不同观测模式数据。在该版本中定义了6种不同类型的数据文件&#xff0c;分别用于存放不同类型的数据&a…

2020/10/23 GPS的数据格式学习

GPS的数据格式学习 一、在使用GPS的通过串口向电脑发送数据的时候&#xff0c;要注意GPS数据线的连接&#xff1b; 1.1 VCC接VCC&#xff1b;&#xff08;VCC表示接电源正极&#xff09; 1.2 GND接GND&#xff1b;&#xff08;GND表示接地或接电源负极&#xff09; 1.3 TX接RX…

GPS数据包格式+数据解析

GPS数据包格式数据解析 一、全球时区的划分&#xff1a; 每个时区跨15经度。以0经线为界向东向西各划出7.5经度&#xff0c;作为0时区。即0时区的经度范围是7.5W——7.5E。从7.5E与7.5W分别向东、向西每15经度划分为一个时区&#xff0c;直到东11区和西11区。东11区最东部的经度…

GPS研究---GPS 数据格式

GPS 数据处理时所采用的观测数据是来自观测的 GPS 接收机。由于接收机的型号很多&#xff0c;厂商设计的数据格式各不相同&#xff0c;国际上为了能统一使用不同接收机的数据&#xff0c; 设计了一种与接收机无关的 RINEX(The Receiver Independent Exchange Format)格式&#…

GPS数据格式的分析

文章目录 前言一、数据格式解析1、GPGGA2、GPRMC3、GPCHC4、Kitti数据集oxts数据 二、驱动1、功能包1.1 解析GPGGA1.2 华测GPCHC 2、ROS相关消息类型2.1 sensor_msgs::NavSatFix2.2 gps_common::GPSFix2.3 sensor_msgs::Imu 3、驱动思路 三、时间1、UTC时间2、时间戳 前言 GPS…

GPS GLONASS数据文件类型解析

GPS & GLONASS数据文件类型解析 一、GPS数据格式 RINEX格式现如今已成为GPS测量应用中的标准数据格式&#xff0c;目前应用最为广泛、最普遍的是RINEX格式的第2个版本&#xff0c;该版本能够用于包括静态和动态GPS测量在内的不同观测模式数据。在该版本中定义了6种不同类…

GPS的数据格式介绍

GPRMC&#xff08;建议使用最小GPS数据格式&#xff09; $GPRMC,<1>,<2>,<3>,<4>,<5>,<6>,<7>,<8>,<9>,<10>,<11><CR><LF> 1) 标准定位时间&#xff08;UTC time&#xff09;格式&#xff1a…

Android ExpandableListView

ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目&#xff0c;这不同于列表视图&#xff08;ListView&#xff09;。ExpandableListView允许有两个层次&#xff1a;一级列表中有二级列表。 比如在手机设置中&#xff0c;对于分类&#xff0c;有很好的效果。手机…

ExpandableListView用法

先上个效果图&#xff1a; 1&#xff0c;我用的fragment import java.util.ArrayList; import java.util.Collections; import java.util.List; import com.dami.student.ui.chatui.adapter.ContactsExpandableListAdapter; import com.dami.student.R; import android.conten…

android expandablelistview简单应用,android ExpandableListView简单例子

android中常常要用到ListView&#xff0c;有时也要用到ExpandableListView&#xff0c;如在手机设置中&#xff0c;对于分类有很好的效果&#xff0c;会用ListView的人一定会用ExpandableListView&#xff0c;因为 ExpandableListView extends ListView的&#xff0c;下面来看个…

android expandablelistview横向,完美实现ExpandableListView二级分栏效果

本文实例为大家分享了ExpandableListView二级分栏效果的具体代码&#xff0c;供大家参考&#xff0c;具体内容如下 对ExpandableListView控件进行封装(未自定义)直接上代码&#xff1a; 通用ViewHolder类&#xff0c;仅在setImageResource中添加代码 package com.svp.haoyan.ex…

android expandablelistview横向,expandableListView 总结

实现效果图&#xff1a; expandableListView groupIndicator 图片默认是在左边&#xff0c;而且比较难看&#xff0c;而我要的是实现groupIndicator 在右边自定义图片&#xff0c; 换图片 最简单的就是直接copy 系统 android:drawable/expander_group ?android:attr/expandab…