Android-宝宝相册(第四次作业)

news2024/9/9 5:31:35

第四次作业-宝宝相册

题目

用Listview建立宝宝相册,相册内容及图片可自行设定,也可在资料文件中获取。给出模拟器仿真界面及代码截图。 (参考例4-8)

创建工程项目

创建名为baby的项目工程,最后的工程目录结构如下图所示:

image-20231027171437431

res/drawable文件中的i1、i2、i3、i4、i5、i6均为图片,即宝宝相册图片,网上自行选取照片即可。

res/layout为文件布局文件,activity_main.xml为自动生成的自定义布局文件,list_item.xml为自定义布局文件

布局文件

  1. 创建自定义布局文件list_item.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/news_thumb"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_margin="5dp"/>
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp">
    
            <TextView
                android:id="@+id/news_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="16sp" />
    
            <TextView
                android:id="@+id/news_info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:layout_marginTop="5dp"/>
    
        </LinearLayout>
    
    </LinearLayout>
    
  2. 修改MainActivity.xml布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
        android:paddingLeft="16dp"
        android:paddingRight="16dp">
    
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </RelativeLayout>
    

MainActivity文件

package com.example.baby;

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;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private ListView listView;

    private SimpleAdapter adapter;


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

        // 假设有一个包含数据的List
        List<Map<String, String>> data = new ArrayList<>();

        Map<String, String> item1 = new HashMap<>();
        item1.put("news_thumb", String.valueOf(R.drawable.i1));	//R.drawable.i1引用照片资源文件
        item1.put("news_title", "毡帽系列");
        item1.put("news_info", "此系列服装有点cute,像不像小车夫。");
        data.add(item1);

        Map<String, String> item2 = new HashMap<>();
        item2.put("news_thumb", String.valueOf(R.drawable.i2));	//R.drawable.i2引用照片资源文件
        item2.put("news_title", "蜗牛系列");
        item2.put("news_info", "宝宝变成了小蜗牛,爬啊爬啊爬啊。");
        data.add(item2);

        Map<String, String> item3 = new HashMap<>();
        item3.put("news_thumb", String.valueOf(R.drawable.i3));
        item3.put("news_title", "小蜜蜂系列");
        item3.put("news_info", "小蜜蜂,嗡嗡嗡,飞到西,飞到东。");
        data.add(item3);

        Map<String, String> item4 = new HashMap<>();
        item4.put("news_thumb", String.valueOf(R.drawable.i4));
        item4.put("news_title", "毡帽系列");
        item4.put("news_info", "此系列服装有点cute,像不像小车夫。");
        data.add(item4);

        Map<String, String> item5 = new HashMap<>();
        item5.put("news_thumb", String.valueOf(R.drawable.i5));
        item5.put("news_title", "蜗牛系列");
        item5.put("news_info", "宝宝变成了小蜗牛,爬啊爬啊爬啊。");
        data.add(item5);

        Map<String, String> item6 = new HashMap<>();
        item6.put("news_thumb", String.valueOf(R.drawable.i6));
        item6.put("news_title", "小蜜蜂系列");
        item6.put("news_info", "小蜜蜂,嗡嗡嗡,飞到西,飞到东。");
        data.add(item6);


        // 定义数据的键与布局文件中组件的映射
        String[] from = {"news_thumb", "news_title", "news_info"};
        int[] to = {R.id.news_thumb, R.id.news_title, R.id.news_info};

        // 创建SimpleAdapter
        adapter = new SimpleAdapter(this, data, R.layout.list_item, from, to);

        // 关联SimpleAdapter与ListView
        listView = findViewById(R.id.list);
        listView.setAdapter(adapter);
      
      	// 为ListView添加一个项目点击监听器,当点击项目时显示对话框
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 获取点击项目的数据
                Map<String, String> itemData = (Map<String, String>) parent.getItemAtPosition(position);

                // 从点击项目的数据中提取文本信息以供对话框使用
                String title = itemData.get("news_title");
                String info = itemData.get("news_info");

                // 创建并显示一个自定义对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle(title)
                        .setMessage(info)
                        .setPositiveButton("确定", null); // 没有操作的确定按钮

                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
    }
}

修改AndroidManifest.xml文件

<activity
    android:name=".MainActivity"
    android:exported="true"
    android:label="SimpleAdapterDemo">		<!--修改导航栏名称-->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

效果展示

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

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

相关文章

Linux 基本语句_8_C语言_文件控制

为了解决多个进程同时操作一个文件&#xff0c;产生一些情况&#xff0c;通常对文件进行上锁&#xff0c;已解决对共享文件的竞争 对打开文件进行各种操作&#xff1a; int fcentl(int fd, int cmd, .../*arg*/如果cmd与锁操作有关&#xff0c;那么fcentl函数的第三个参数就要…

从Mysql架构看一条查询sql的执行过程

1. 通信协议 我们的程序或者工具要操作数据库&#xff0c;第一步要做什么事情&#xff1f; 跟数据库建立连接。 首先&#xff0c;MySQL必须要运行一个服务&#xff0c;监听默认的3306端口。在我们开发系统跟第三方对接的时候&#xff0c;必须要弄清楚的有两件事。 第一个就是通…

38基于matlab的期货预测,利用PSO优化SVM和未优化的SVM进行对比,得到实际输出和期望输出结果。

基于matlab的期货预测&#xff0c;利用PSO优化SVM和未优化的SVM进行对比&#xff0c;得到实际输出和期望输出结果。线性核函数、多项式、RBF核函数三种核函数任意可选&#xff0c;并给出均方根误差&#xff0c;相对误差等结果&#xff0c;程序已调通&#xff0c;可直接运行。 3…

哈希算法:哈希算法在分布式系统中有哪些应用?

文章来源于极客时间前google工程师−王争专栏。 哈希算法除了上篇的四个应用&#xff08;安全加密、数据校验、唯一标识、散列函数&#xff09;&#xff0c;还有三种应用&#xff1a;负载均衡、数据分片、分布式存储。 这三个应用都跟分布式系统有关。哈希算法是如何解决这些分…

Nginx的进程结构实例演示

可以参考《Ubuntu 20.04使用源码安装nginx 1.14.0》安装nginx 1.14.0。 nginx.conf文件中worker_processes 2;这条语句表明启动两个worker进程。 sudo /nginx/sbin/nginx -c /nginx/conf/nginx.conf开启nginx。 ps -ef | grep nginx看一下进程情况。 sudo /nginx/sbin/ng…

Java8与JDK1.8与JDK8之间的关系是什么?

1.Java8等价于JDK8 2.JDK8或者JDK1.8是由于自从JDK1.5/JDK5命名方式改变后遗留的历史问题。所以JDK8或者JDK1.8是等价的。 2004年9月30日&#xff0c;J2SE1.5发布。为了表示该版本的重要性&#xff0c;J2SE 1.5更名为Java SE 5.0&#xff0c;从此开始&#xff0c;如下图像jav…

Go学习第十四章——Gin请求与响应

Go web框架——Gin请求与响应 1 响应1.1 String1.2 JSON&#xff08;*&#xff09;1.3 HTML&#xff08;*&#xff09;1.4 XML1.5 文件&#xff08;*&#xff09; 2 请求2.1 请求参数查询参数 (Query)动态参数 (Param)表单参数 (PostForm)原始参数 (GetRawData) 2.2 请求头2.3 …

JAVA:集合框架常见的面试题和答案

1、List接口的常见实现类有哪些&#xff1f; 答&#xff1a; 常见的List接口实现类包括&#xff1a; ArrayList: 基于动态数组实现的List&#xff0c;支持快速随机访问。LinkedList: 基于链表实现的List&#xff0c;支持快速的插入和删除操作。Vector: 一个线程安全的动态数组…

GnuTLS recv error (-110): The TLS connection was non-properly terminated

ubuntu git下载提示 GnuTLS recv error (-110): The TLS connection was non-properly terminated解决方法 git config --global --unset http.https://github.com.proxy

异步请求池——池式组件

前言 本文详细介绍异步请求池的实现过程&#xff0c;并使用DNS服务来测试异步请求池的性能。            两个必须牢记心中的概念&#xff1a; 同步&#xff1a;检测IO 与 读写IO 在同一个流程里异步&#xff1a;检测IO 与 读写IO 不在同一个流程 同步请求 与 异步请求…

C++入门精讲——入门看完这一篇就够了

文章目录 前言1. C发展历史2. 关键字3. 命名空间3.1 命名空间的概念3.2 命名空间的定义3.3 命名空间的使用 4. C输入、输出5. 缺省参数5.1 全缺省5.2 半缺省 6. 函数重载6.1 几种不同类型的函数重载6.2 函数重载的原理——名字修饰(name Mangling)6.2.1 C程序为什么不支持函数重…

SpringCore完整学习教程5,入门级别

本章从第6章开始 6. JSON Spring Boot提供了三个JSON映射库的集成: Gson Jackson JSON-B Jackson是首选的和默认的库。 6.1. Jackson 为Jackson提供了自动配置&#xff0c;Jackson是spring-boot-starter-json的一部分。当Jackson在类路径上时&#xff0c;将自动配置Obj…

【目标检测】Visdrone数据集和CARPK数据集预处理

之前的博文【目标检测】YOLOv5跑通VisDrone数据集对Visdrone数据集简介过&#xff0c;这里不作复述&#xff0c;本文主要对Visdrone数据集和CARPK数据集进行目标提取和过滤。 需求描述 本文需要将Visdrone数据集中有关车和人的数据集进行提取和合并&#xff0c;车标记为类别0&…

软考系列(系统架构师)- 2010年系统架构师软考案例分析考点

试题一 软件系统架构选择 【问题1】&#xff08;7分&#xff09; 在实际的软件项目开发中&#xff0c;采用恰当的架构风格是项目成功的保证。请用200字以内的文字说明什么是软件架构风格&#xff0c;并对主程序-子程序和管道-过滤器这两种架构风格的特点进行描述。 软件架构风…

【C语言数据结构——————排序(1万字)】

文章目录 排序的概念 常见排序算法分类冒泡排序 时间复杂度稳定性 原理实现插入排序 时间复杂度稳定性实现选择排序 时间复杂度稳定性实现希尔排序 时间复杂度稳定性希尔排序的算法思想实现 优化快速排序 时间复杂度空间复杂度稳定性实现 三数取中优化归并排序 时间复杂度空间复…

windows下OOM排查

如下有一段代码 package com.lm.demo.arthas.controller;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.util.A…

Notepad++安装插件和配置快捷键

Notepad是一款轻量级、开源的文件编辑工具&#xff0c;可以编辑、浏览文本文件、二进制文件、.cpp、.java、*.cs等文件。Notepad每隔1个月&#xff0c;就有一个新版本&#xff0c;其官网是&#xff1a; https://github.com/notepad-plus-plus/notepad-plus-plus。这里介绍其插件…

maven之父子工程版本控制案例实战,及拓展groupId和artifactId的含义

<parent>标签 用于父子工程项目&#xff0c;什么是父子工程&#xff1f; 顾名思义&#xff0c;maven父子项目是一个有一个父项目&#xff0c;父项目下面又有很多子项目的maven工程&#xff0c;当然&#xff0c;子项目下面还可以添加子项目&#xff0c;从而形成一个树形…

GPS学习(一):在ROS2中将GPS经纬度数据转换为机器人ENU坐标系,在RVIZ中显示坐标轨迹

文章目录 一、GPS模块介绍二、坐标转换转换原理参数解释&#xff1a; 增加回调函数效果演示 本文记录在Ubuntu22.04-Humbel中使用NMEA协议GPS模块的过程&#xff0c;使用国产ROS开发板鲁班猫(LubanCat )进行调试。 一、GPS模块介绍 在淘宝找了款性价比较高的轮趣科技GPS北斗双…

AD7321代码SPI接口模数转换连接DAC0832输出verilog

名称&#xff1a;AD7321代码12位ADC&#xff0c;SPI接口模数转换连接DAC0832输出 软件&#xff1a;QuartusII 语言&#xff1a;VHDL 代码功能&#xff1a; 使用VHDL语言编写代码&#xff0c;实现AD7321的控制&#xff0c;将模拟信号转换为数字信号&#xff0c;再经过处理后…