ListView使用方法

article/2025/9/26 9:42:22

ListView使用方法总结


- 直接使用ListView组件创建列表

- 通过Activity继承ListActivity创建

- 定制ListView界面


直接使用ListView组件创建列表

通过数组资源文件指定列表项

  1. 先在XML布局文件中添加ListView标志,设置好相关属性;在values下创建数组资源文件arrays.xml,添加字符串数组intype
        <resources><string-array name="intype"><item>关机</item><item>飞行模式</item><item>重新启动</item><item>数据网络模式</item></string-array></resources>

在ListView布局中使用”android:entries =@arrays/intype”指定列表项

<ListView
    android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/list1"android:entries="@array/intype">
</ListView>

注意:资源数组只能指定纯文字的列表项
效果:
这里写图片描述


2 . 第二种是使用适配器指定列表项,去掉布局中属性”android:entries =@arrays/intype”,然后在Activity 文件中添加ArrayAdapter (通常用于纯文本列表项) 调用ArrayAdapter.createFromResource()创建适配器

 ListView lst=findViewById(R.id.list1);ArrayAdapter <CharSequence> ad = ArrayAdapter.createFromResource(this,R.array.intype,android.R.layout.simple_dropdown_item_1line);lst.setAdapter(ad);

其中 android.R.layout.simple_dropdown_item_1line用于指定ListView外观形式
常用的有以下几个:
- simple_list_item_1:普通文本
- simple_list_item_2:普通文本,字体较大
- simple_list_item_checked:带有勾选框的列表项
- simple_list_item_multiple_choice:带有多选框文本
- simple_list_item_single_choice:带有单选按钮的文本


通过Activity继承ListActivity创建

将Activity继承ListActivity,删除默认的布局文件,在onCreate()方法中创建适配器,调用setListAdapter()添加

public class MainActivity extends ListActivity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);String[] ctype = new String[]{"新增支出", "新增收入", "我的支出", "我的收入", "数据管理", "系统设置"};ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, ctype);setListAdapter(adapter);getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);}protected void onListItemClick(ListView l, View v, int position, long id) {super.onListItemClick(l, v, position, id);String result=l.getItemAtPosition(position).toString();Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();}
}

效果:
这里写图片描述


定制ListView界面

1、在默认的布局文件中添加ListView标志

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

2、src/layout中新建XML文件gvitem.xml,用于设置每一个列表的布局样式

    <LinearLayout
            android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><ImageView
                android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/tongxuntu"android:scaleType="centrInside"android:layout_margin="10dp"/><TextView
                android:id="@+id/tonngxuntext"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="5dp"android:layout_gravity="center_vertical"android:textSize="13sp"android:textColor="#3b3b3b"/></LinearLayout>

3、在Activity文件中,实例化一个simpleAdapter适配器, List集合类用于添加图标、文本

 public void onActivityCreated(@Nullable Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);list=InitData();simpleAdapter=new SimpleAdapter(getActivity(),list,R.layout.frag2_gvitem,new String[]{"image","title"},new int[]{R.id.tongxuntu,R.id.tonngxuntext});listView.setAdapter(simpleAdapter);}private List<Map<String,Object>> InitData() {String [] name=new String[]{"新的朋友","群聊","标签","公众号"};//图片资源int [] image=new int[]{R.mipmap.friend,R.mipmap.group,R.mipmap.lable,R.mipmap.pub};List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();for (int i=0;i<image.length;i++){Map<String,Object> map=new HashMap<String, Object>();map.put("image",image[i]);map.put("title",name[i]);list.add(map);}return list;}

效果:
这里写图片描述


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

相关文章

Android—— ListView 的简单用法及定制ListView界面

一、ListView的简单用法 2. 训练目标 1) 掌握 ListView 控件的使用 2) 掌握 Adapter 桥梁的作用 实现步骤&#xff1a; 1&#xff09;首先新建一个项目&#xff0c; 并让ADT 自动帮我们创建好活动。然后修改activity_main.xml 中的代码&#xff0c;如下所示&#xff1a; &…

QT listView学习

文章目录 listViewdemo说明demo演示model定义委托 QStyledItemDelegate总结 listView listView 对比 tableView 、 treeView来说&#xff0c;最大的不同就是数据结构的不同。treeView是像树一样的层次结构&#xff0c;而listView则就是像链表一样的结构 跟之前的treeView&…

还在用ListView?

还在用Lisview&#xff1f;RecyclerView都已经出来一年多了&#xff01; 想必大家多或多或少的接触过或者了解过RecyclerView&#xff0c;为什么没有用起来&#xff0c;原因大概如下&#xff1f; ListView我用的挺好的&#xff0c;为什么要换RecyclerView&#xff1f;ListView…

ListView用法

ListView是用于显示数据的&#xff0c;先在窗体中拉一个lisview控件&#xff0c;还有一些新增、修改、删除、查询按钮和文本框&#xff0c;控件名称为listview,按钮为btnInsert,btnUpate,btnDeleteOne,btnDelete,btnSelect,文本框的名称为txtName,txtSex,txtPhone,txtAddress,设…

ListView的基础用法

最近学到ListView和RecyclerView&#xff0c;感觉有点难理解&#xff0c;于是自己找到了篇文章&#xff0c;感觉写的挺详细的&#xff08;文章链接在文末&#xff09;&#xff0c;然后自己再整理敲了跑了一遍&#xff0c;总结了一下&#xff0c;方便自己以后回头温习。 一个Li…

Android(14) ArrayAdapter(数组适配器)的三种方法

ArrayAdapter数组适配器用于绑定格式单一的数据&#xff0c;数据源可以是集合或者数组 列表视图(ListView)以垂直的形式列出需要显示的列表项。 实现过程&#xff1a;新建适配器->添加数据源到适配器->视图加载适配器 第一种&#xff1a;直接用ListView组件创建 列表…

Android——列表视图(ListView)

1、什么是ListView&#xff1f;它可以实现怎样的功能&#xff1f; 列表视图是android中最常用的一种视图组件&#xff0c;它以垂直列表的形式列出需要显示的列表项。手机屏幕空间有限&#xff0c;能显示的内容不多。可以借助ListView来显示更多、更丰富的内容。ListView允许用…

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…