Android之ListView

news2025/6/3 7:55:39

1:简单列表(ArrayAdapter)

1:运行的结果:

2:首先在MyListView里面创建一个按钮,点击的时候进行跳转。

这里让我吃惊的是,Button里面可以直接设置onClick = .java里面的方法。

也即是点击这个按钮之后就会去调用这个方法。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyListView">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="10dp"
        android:orientation="vertical"
        android:paddingLeft="10dp">


        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Array列表"
            android:onClick="toArrayListTest"
            android:textAllCaps="false"/>

    </LinearLayout>
</ScrollView>

3:在MyListView.java里面编写跳转代码

package com.findyou.mylistview;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MyListView extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_list_view);

    }

    public void toArrayListTest(View view) {
        Intent intent = new Intent(this, ArrayListActivity.class);
        startActivity(intent);
    }
}

这里用的也是Intent, 第一个写的这packageContext, 第二个表示你要跳转的目的。然后就开启跳转startActivity(传入intent)。

4:在ArrayListActivity里面先准备好 ListView

<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ArrayListActivity"
    android:orientation="vertical"
    >
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>



</LinearLayout>

 5:在ArrayListActivity.java里面编写核心代码

首先去拿到.xml界面里面的ListView,然后创建一个列表进行填充数据。再需要一个适配器,适配器里面需要的参数有,你这个填充的需要什么样的格式?(用的是官方的),以及填充的数据是什么?然后把获得是ListView进行setAdapter,启动。

对ListView里面的Item进行点击和长按监听。里面重写的参数是包括position的。

package com.findyou.mylistview;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class ArrayListActivity extends AppCompatActivity {

    private ListView listView;

    // 下面这个去提供数据
    private List<String> mStringList;

    private ArrayAdapter<String> mArrayAdapter; // 需要一个适配器

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_array_list);
        listView = findViewById(R.id.lv);

        // 数据的初始化
        mStringList = new ArrayList<>();
        for(int i = 1; i <= 50;  ++ i ) {
            mStringList.add("这是条目: " + i);
        }
        // 第二个参数表示的是 这个的布局是什么?
        mArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mStringList);
        listView.setAdapter(mArrayAdapter); // 填充数据

        // 设置点击的监听
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(ArrayListActivity.this, "你点击了" + position, Toast.LENGTH_SHORT).show();
            }
        });

        // 设置长按的监听
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(ArrayListActivity.this, "你长按了" + position, Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
}

2:simpleList列表(图文)

 1:运行的结果:

2:首先也是写一个activity_simple_list.xml, 写一个大的ListView先撑大

<?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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SimpleListActivity"
    android:orientation="vertical"
    >


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



</LinearLayout>

3:在MyListView.java里面补充跳转代码

    public void toSimplArrayListTest(View view) {
        Intent intent = new Intent(this, SimpleListActivity.class);
        startActivity(intent);
    }

4:在SimpleListActivity.java里面配置适配器(adapter)等 

package com.findyou.mylistview;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SimpleListActivity extends AppCompatActivity {

    private ListView listView;
    private SimpleAdapter simpleAdapter;
    private List<Map<String, Object> >list; // 存放数据的

    private int[] imags =  {
            R.drawable.test1,
            R.drawable.test2,
            R.drawable.test3,
            R.drawable.test4,
            R.drawable.test5,
            R.drawable.test6,
            R.drawable.test7,
            R.drawable.test8,
            R.drawable.test9,
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_list);
        listView = findViewById(R.id.lv);
        list = new ArrayList<>();
        for(int i = 1; i <= 50; i ++ ) {
            Map<String, Object> mp = new HashMap<>();
            mp.put("img", imags[i % 9]);
            mp.put("title", "标题" + i);
            mp.put("content", "内容" + i);
            list.add(mp); // 存放全部的数据
        }
        simpleAdapter = new SimpleAdapter(this, list,
                R.layout.list_item_layout,
                new String[] {"img", "title", "content"},
                new int[] {
                        R.id.iv_img, R.id.tv_title, R.id.tv_content
                    });


        // 上面的倒数两个参数 表示是 从哪到哪 from to, to 表示的是xml里面的id

        listView.setAdapter(simpleAdapter);

    }
}

这里适配器simpleAdapter,里面的参数

this: 表示的是当前

list:  存放数据的(data), 但是这个里面的要求是List<Map<String, *>> 的形式。

R.layout.list_item_layout: 这个是每一个item的布局是什么样的。

 new String[] {"img", "title", "content"}: 表示你data里面的数据注意顺序(from)。

new int[] 表示的R.id,你要插入到的模板里面的哪一个id(to)。

 R.layout.list_item_layout里面的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingTop="5dp"
    android:paddingRight="10dp"
    android:paddingBottom="5dp">

    <ImageView
        android:id="@+id/iv_img"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher_background"/>


    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/iv_img"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="雨中漫步"
        android:textSize="20sp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/iv_img"
        android:ellipsize="end"
        android:maxLines="3"
        android:text="人生就像一场旅行,不必在乎目的地,在乎的是沿途的风景以及看风景的心情,让心灵去旅行"
        android:textSize="16sp"/>


</RelativeLayout>

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2394594.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

《Spring Cloud Gateway 快速入门:从路由到自定义 Filter 的完整教程》​

1.网关介绍 在前面的学习中&#xff0c;我们通过Eureka和Nacos解决了辅助注册&#xff0c;使用Spring Cloud LoadBalance解决了负载均衡的问题&#xff0c;使用OpenFeign解决了远程调用的问题。 但是当前的所有微服务的接口都是直接对外暴露的&#xff0c;外部是可以直接访问…

第3节 Node.js 创建第一个应用

Node.js 非常强大&#xff0c;只需动手写几行代码就可以构建出整个HTTP服务器。事实上&#xff0c;我们的Web应用以及对应的Web服务器基本上是一样的。 在我们创建Node.js第一个"Hello, World!"应用前&#xff0c;让我们先了解下Node.js应用是由哪几部分组成的&…

我们来学mysql -- “数据备份还原”sh脚本

数据备份&还原 说明执行db_backup_cover.sh脚本 说明 环境准备&#xff1a;来源数据库(服务器A)&#xff1b;目标数据库(服务器B)dbInfo.sh脚本记录基本信息 来源库、目标库的ip、port及执行路径 # MySQL 客户端和 mysqldump 的路径 MYSQL_CLIENT"/work/oracle/mysql…

【排序算法】快速排序详解--附详细流程代码

快速排序算法 介绍 快速排序&#xff08;Quick Sort&#xff09;是一种高效的分治排序算法&#xff0c;由英国计算机科学家 Tony Hoare 于 1960 年提出。它是实际应用中最常用的排序算法之一。快速排序的基本思想是&#xff1a;选择一个"基准"&#xff08;pivot&am…

解决各个系统报错TDengine:no taos in java.library.path问题

windows 系统解决办法 在本地上安装一个TD的Windows客户端&#xff0c;注意安装的客户端版本一定要和服务端TD版本完全一致。&#xff08;或者将 C:\TDengine\driver\taos.dll 拷贝到 C:\Windows\System32\ 目录下&#xff09; 客户端各个历史版本下载链接&#xff1a;TDengin…

java helloWord java程序运行机制 用idea创建一个java项目 标识符 关键字 数据类型 字节

HelloWord public class Hello{public static void main(String[] args) {System.out.print("Hello,World!");} }java程序运行机制 用idea创建一个java项目 建立一个空项目 新建一个module 注释 标识符 关键字 标识符注意点 数据类型 public class Demo02 {public st…

免费文本转语音工具体验:祈风TTS使用

简介&#xff1a;语音生成的另一种方式 现在很多人通过视频记录生活&#xff0c;表达观点。拍摄剪辑不难&#xff0c;配音成了常见难题。部分人对自己的声音不够自信&#xff0c;也有人在特定场景下不便出声。文本转语音工具可以成为解决方案。 常见的TTS&#xff08;Text To…

JS和TS的区别

JavaScript 与 TypeScript 的主要区别和特性对比 1. 基础定义 JavaScript 是一种动态、弱类型的编程语言&#xff0c;广泛应用于前端开发以及通过 Node.js 扩展到后端开发。TypeScript 则是 JavaScript 的超集&#xff0c;它在 JavaScript 的基础上添加了静态类型系统和其他增…

Python实现P-PSO优化算法优化BP神经网络分类模型项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档&#xff09;&#xff0c;如需数据代码文档可以直接到文章最后关注获取。 1.项目背景 随着人工智能技术的快速发展&#xff0c;神经网络在分类任务中展现了强大的性能。BP&#xff08;Back Propagation&…

Linux --进度条小程序更新

这里使用随机数来模拟下载量&#xff0c;来实现一个下载进度更新的小程序 main.c 的代码&#xff0c;其中downlod这个函数使用的是函数指针&#xff0c;如果有多个进度条函数可以传入进行多样化的格式下载显示&#xff0c;还需要传入一个下载总量&#xff0c;每次"下载以…

关于镜像如何装进虚拟机

本篇文章为感谢小仙猪老师特别编写 本篇文章仅以Ubuntu为例 目录 创建虚拟机 汉化 如果没有China选项 检查网络 创建虚拟机 第一步&#xff0c;创建虚拟机 因为&#xff0c;第一个选项是会把虚拟机的文件放在c盘因此&#xff0c;这里博主选择自定义&#xff0c;然后下一…

智慧体育馆数字孪生,场馆管理智能化

图扑数字孪生智慧体育馆可视化管理平台。通过高精度三维建模&#xff0c;对体育馆建筑结构、设施设备等进行 1:1 虚拟映射&#xff0c;全方位还原场馆物理实体。系统集成多维度传感器数据&#xff0c;实现对人流量、客流密度、区域拥堵指数等信息的实时采集与分析&#xff0c;动…

回归算法模型之线性回归

哈喽&#xff01;我是 我不是小upper&#xff5e; 今天来和大家聊聊「线性回归」—— 这是机器学习里最基础、最直观的算法之一&#xff0c;咱们用一个超简单的例子就能搞懂它&#xff01; 先看一个生活场景 假设你是房产中介&#xff0c;遇到一个灵魂拷问&#xff1a; 客户有…

【深度学习】10. 深度推理(含链式法则详解)RNN, LSTM, GRU,VQA

深度推理&#xff08;含链式法则详解&#xff09;RNN, LSTM, GRU&#xff0c;VQA RNN 输入表示方式 在循环神经网络&#xff08;Recurrent Neural Network, RNN&#xff09;中&#xff0c;我们处理的是一段文字或语音等序列数据。对于文本任务&#xff0c;输入通常是单词序列…

【Qt】Bug:findChildren找不到控件

使用正确的父对象调用 findChildren&#xff1a;不要在布局对象上调用 findChildren&#xff0c;而应该在布局所在的窗口或控件上调用。

【linux】linux进程概念(四)(环境变量)超详细版

小编个人主页详情<—请点击 小编个人gitee代码仓库<—请点击 linux系列专栏<—请点击 倘若命中无此运&#xff0c;孤身亦可登昆仑&#xff0c;送给屏幕面前的读者朋友们和小编自己! 目录 前言一、基本概念二、认识常见的几个环境变量echo $ 查看某个环境变量env 显示…

从零开始的二三维CAD|CAE软件: 解决VTK,DICOM体素化-失效问题.

背景: 在从零开始的二三维软件开发中, 需要加载CT的dicoms影像文件, 并将其序列化之后的数据,体素化 可惜..vtk的c#库,将其体素化的时候,竟然失败... 使用vtkDicomReader ,设置 Dicom文件夹读取,竟然不停的失败...从网上找了一些版本.也没啥可用的资料... 解决办法: 直接…

【计算机网络】应用层协议Http——构建Http服务服务器

&#x1f525;个人主页&#x1f525;&#xff1a;孤寂大仙V &#x1f308;收录专栏&#x1f308;&#xff1a;计算机网络 &#x1f339;往期回顾&#x1f339;&#xff1a; 【Linux笔记】——进程间关系与守护进程 &#x1f516;流水不争&#xff0c;争的是滔滔不息 一、Http协…

linux版本vmware修改ubuntu虚拟机为桥接模式

1、先打开linux版本vmware操作界面 2、设置虚拟路由编辑器的桥接模式 输入账号密码 自动模式 不需要进行任何操作 3、修改虚拟机设置网络模式为桥接模式 然后save保存一下配置 4、现在进入虚拟机查看ens33配置 网卡启动但是没有ip 5、自己进行设置修改ubuntu网络配置文件 cd …

从0到1上手Trae:开启AI编程新时代

摘要&#xff1a;字节跳动 2025 年 1 月 19 日发布的 Trae 是一款 AI 原生集成开发环境工具&#xff0c;3 月 3 日国内版推出。它具备 AI 问答、代码自动补全、基于 Agent 编程等功能&#xff0c;能自动化开发任务&#xff0c;实现端到端开发。核心功能包括智能代码生成与补全、…