移动技术开发:备忘录

news2024/10/22 8:32:41

1 实验名称

       备忘录

2 实验目的

       掌握SQLite数据库的基本操作,实现备忘录基本功能。

3 实验源代码

布局文件代码:

(1)activity_main.xml

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="主题:"
                android:textSize="20sp" />
            <EditText
                android:id="@+id/et_subject"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </TableRow>

        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="内容:"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/et_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minLines="4"/>
        </TableRow>

        <TableRow>

            <Button
                android:id="@+id/btn_chooseDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="选择时间"/>

            <EditText
                android:id="@+id/et_data"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:editable="false"/>
        </TableRow>
    </TableLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        
        <Button
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加"/>

        <Button
            android:id="@+id/btn_query"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="查询"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            style="@style/TextView"
            android:layout_width="40dp"
            android:text="序号"
            android:textColor="#000000"/>

        <TextView
            style="@style/TextView"
            android:layout_width="50dp"
            android:text="主题"
            android:textColor="#000000"/>

        <TextView
            style="@style/TextView"
            android:layout_width="110dp"
            android:text="内容"
            android:textColor="#000000"/>

        <TextView
            style="@style/TextView"
            android:layout_width="100dp"
            android:text="时间"
            android:textColor="#000000"/>
    </LinearLayout>

    <ListView
        android:id="@+id/lv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

(2)result.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">
    <TextView
        android:id="@+id/tv_num"
        android:layout_width="40dp"
        style="@style/TextView"
        android:text="序号"/>
    <TextView
        android:id="@+id/tv_subject"
        android:layout_width="50dp"
        style="@style/TextView"
        android:text="主题"/>
    <TextView
        android:id="@+id/tv_content"
        android:layout_width="40dp"
        style="@style/TextView"
        android:text="内容"/>
    <TextView
        android:id="@+id/tv_date"
        android:layout_width="40dp"
        style="@style/TextView"
        android:text="时间"/>

</LinearLayout>

Java代码:

(1)MainActivity

package com.example.memorandumtest;

import android.app.DatePickerDialog;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

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

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private EditText et_subject = null;
    private EditText et_content = null;
    private EditText et_date = null;

    private Button btn_Choosedate = null;
    private Button btn_add = null;
    private Button btn_query = null;

    private LinearLayout title = null;
    private ListView lv_result = null;

    //定义数据库工具类对象
    private MyDatabaseHelper myDatabaseHelper = null;
    //定义数据库对象
    private SQLiteDatabase sqLiteDatabase = null;

    private MyListener myListener = null;

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

        et_subject = findViewById(R.id.et_subject);
        et_content = findViewById(R.id.et_content);
        et_date = findViewById(R.id.et_data);

        btn_Choosedate = findViewById(R.id.btn_chooseDate);
        btn_add = findViewById(R.id.btn_add);
        btn_query = findViewById(R.id.btn_query);

        title = findViewById(R.id.title);
        lv_result = findViewById(R.id.lv_result);

        btn_Choosedate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建一个日历的对象
                Calendar calendar = Calendar.getInstance();
                new DatePickerDialog(
                        MainActivity.this,
                        new DatePickerDialog.OnDateSetListener() {//日期选择监听器
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                et_date.setText(year+"-"+(month+1)+"-"+dayOfMonth);
                            }
                        },
                        calendar.get(Calendar.YEAR),//设置当前的年份
                        calendar.get(Calendar.MONTH),//设置当前的月份
                        calendar.get(Calendar.DAY_OF_MONTH)//设置当前的日期
                ).show();
            }
        });

        //为添加和查询按钮绑定监听器对象
        myListener =new MyListener();
        btn_add.setOnClickListener(myListener);
        btn_query.setOnClickListener(myListener);

    }

    private class MyListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {

            //创建数据库工具类对象
            myDatabaseHelper = new MyDatabaseHelper(MainActivity.this,
                    "memento.db",
                    null, 1);
            //创建数据库对象
            sqLiteDatabase = myDatabaseHelper.getReadableDatabase();

            //把用户输入的数据取出来
            String subjectStr = et_subject.getText().toString().trim();
            String contentStr = et_content.getText().toString().trim();
            String dateStr = et_date.getText().toString().trim();


            if (v.getId() == R.id.btn_add) {
                //调用向数据库中添加记录的方法
                addMemento(sqLiteDatabase, subjectStr, contentStr, dateStr);
            } else if (v.getId() == R.id.btn_query) {

                //调用查询数据库中的记录的方法
                Cursor cursor = queryMemento(sqLiteDatabase, subjectStr, contentStr, dateStr);
                //将查询结果在列表中显示
                //创建一个简单游标适配器对象
                SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(
                        MainActivity.this,
                        R.layout.result,//列表中每一项的布局文件
                        cursor,
                        new String[]{"_id", "subject", "content", "date"},//数据表中的字段信息
                        new int[]{R.id.tv_num, R.id.tv_subject, R.id.tv_content, R.id.tv_date}
                );
                //让列表适用适配器对象
                lv_result.setAdapter(simpleCursorAdapter);

            }
        }
    }

    //向数据库添加记录的方法
    private void addMemento(SQLiteDatabase database,String subject,String content,String date){
        database.execSQL("insert into memento_tb values(null,?,?,?)",
                new String[]{subject,content,date});

        //清空数据方便用户下一次输入
        et_subject.setText("");
        et_content.setText("");
        et_date.setText("");
    }

    //查询数据库记录的方法
    private Cursor queryMemento(SQLiteDatabase sqLiteDatabase,String subject,String content,String date){
        Cursor cursor = sqLiteDatabase.rawQuery("select * from memento_tb where subject like ? and content like ? and date like ?",
                new String[]{"%"+subject+"%","%"+content+"%","%"+date+"%"});
        return cursor;
    }
}

(2)MyDatabaseHelper

package com.example.memorandumtest;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class MyDatabaseHelper extends SQLiteOpenHelper {

    //建表语句
    final String CREATE_TABLE_SQL = "create table memento_tb (_id integer primary key autoincrement,subject,content,date)";
    public MyDatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //执行建表语句,创建memento表
        db.execSQL(CREATE_TABLE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        System.out.println("------"+oldVersion+"-------->"+newVersion);
    }
}

4 实验运行结果图

5 实验总结

       首先还是敲定布局文件,一共两个布局文件,第一个是备忘录的整体布局文件,第二个是显示查询结果的布局文件。

       接着完成Java代码。在写Java代码的过程中,在选择时间按钮的实现时,先创建一个日历对象,创建日期选择监听器,然后设置当前的年月日,其中月份是从0开始的,所以写代码的时候要加1;关于数据的存储与查找,先创建数据库类工具对象和数据库对象,然后通过调用数据库中添加记录和查询记录的方法来实现;对于查询结果在列表中的显示,创建一个简单游标适配器对象,让列表适用适配器对象。

       这个实验还存在一个问题,那就是备忘录的输入只能用英文,而无法采用中文输入,这个问题尚未解决。

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

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

相关文章

在IMX6ul中,使用GPT定时器实现高精度延时

在上一节讲解过了。IMX6UL中的EPIT定时器&#xff0c;这一节我们讲解通用寄存器 在STM32中&#xff0c;我们使用过SYSTICK来实现高精度的延时。IMX6U当中没有SYSTICK定时器&#xff0c;但是IMX6U有其他的定时器&#xff0c;前面的EPIT以及这一节我们将要使用的GPT定时器…

算法备案必须做吗?不做有什么风险?

算法备案是一种强制性备案制度&#xff0c;旨在保障算法技术的合法性和合规性。 《互联网信息服务算法推荐管理规定》第二十四条明确规定应当在10个工作日内备案&#xff0c;发生变更的在10个工作日内完成变更&#xff0c;注销的在20个工作日内完成注销。 ​未履行备案的&…

共筑华芯|首届“SEMiBAY湾芯展”龙华区科技创新局助力华芯邦科技携第四代半导体芯星亮相湾区半导体产业生态博览会,诚邀您莅临参观指导

在深圳市政府指导和深圳市发展改革委支持下&#xff0c;深圳市半导体与集成电路产业联盟携手深圳市重大产业投资集团有限公司共同主办的首届“SEMiBAY湾芯展”——湾区半导体产业生态博览会&#xff0c;将于今年10月16日至18日盛大举行。 本次展会以“芯动未来&#xff0c;共创…

MYSQL8.0.24数据库登录时 报错 ERROR 1045 (28000) / MYSQL8.0.24数据库多次修改密码无效

文章目录 前提:失败方案一:修改密码失败方案二:失败方案三:最终解决方案:前提: 在没有使用电脑上的mysql一段时间之后,忘记了mysql的root账号密码。然后登录怎么登录都登录不上,在重置了密码之后。发现一直报这个错误 ERROR 1045 (28000) : Access denied for user ‘…

ChatTTS在Windows电脑的本地部署与远程生成音频详细实战指南

文章目录 前言1. 下载运行ChatTTS模型2. 安装Cpolar工具3. 实现公网访问4. 配置ChatTTS固定公网地址 前言 本篇文章主要介绍如何快速地在Windows系统电脑中本地部署ChatTTS开源文本转语音项目&#xff0c;并且我们还可以结合Cpolar内网穿透工具创建公网地址&#xff0c;随时随…

深入计算机语言之C++:类与对象(上)

&#x1f511;&#x1f511;博客主页&#xff1a;阿客不是客 &#x1f353;&#x1f353;系列专栏&#xff1a;从C语言到C语言的渐深学习 欢迎来到泊舟小课堂 &#x1f618;博客制作不易欢迎各位&#x1f44d;点赞⭐收藏➕关注 前面我们学习了关于c语言的一些基础知识&#xff…

Go语言gRPC快速入门

文章目录 前言gRPC是什么Go语言的gRPC技术栈准备工作接口定义代码生成服务端代码编写客户端代码编写效果演示完整代码链接最后 前言 你好&#xff0c;我是醉墨居士&#xff0c;这篇博客想帮助初学者能够快速入门gRPC&#xff0c;希望能够为你节省宝贵的时间&#xff0c;让时间…

无线网络基础

文章目录 无线组网胖AP瘦AP胖瘦AP的区别组网方式连接方式CAPWAP概念 无线组网 WLAN&#xff1a;无线局域网 蜂窝网络&#xff1a;无线广域网 802.11&#xff0c;无线网络封装的报文头部 WLAN是一种基于IEEE 802.11标准的无线局域网技术 802.11标准聚焦在TCP/IP对等模型的下两层…

算力提升10倍,特斯拉Cybercab预示了半导体行业哪些方向?

北京时间10月11日上午&#xff0c;备受世界瞩目的“We&#xff0c;Robot”发布会成功举行。现场&#xff0c;特斯拉正式发布了被命名为Cybercab的特斯拉Robotaxi&#xff08;无人驾驶出租车&#xff09;&#xff0c;以及Robovan&#xff08;无人驾驶厢式货车&#xff09;。 这次…

Harmony OS原生端渲染RTMP流功能实现

前段时间公司收到鸿蒙的邀请&#xff0c;希望我们加入鸿蒙的生态应用。领导要求我们将原先安卓SDK实现的功能在鸿蒙生态上1&#xff1a;1还原。相信这也是当前很多公司在做的事情。 我们项目原先有获取硬件实时视频流展示的需求&#xff0c;这块功能也耗费了两周的时间才打通测…

springboot 整合 快手 移动应用 授权 发布视频 小黄车

前言&#xff1a; 因快手文档混乱&#xff0c;官方社区技术交流仍有很多未解之谜&#xff0c;下面3种文档的定义先区分。 代码中的JSON相关工具均用hutool工具包 1.快手 移动双端 原生SDK 文档https://mp.kuaishou.com/platformDocs/develop/mobile-app/ios.html 2.快手 Api 开…

每日OJ题_牛客_chika和蜜柑_TopK_C++_Java

目录 牛客_chika和蜜柑_TopK 题目解析 C代码 Java代码 牛客_chika和蜜柑_TopK chika和蜜柑 (nowcoder.com) 描述&#xff1a; chika很喜欢吃蜜柑。每个蜜柑有一定的酸度和甜度&#xff0c;chika喜欢吃甜的&#xff0c;但不喜欢吃酸的。 一共有n个蜜柑&am…

万物皆可浮雕,comfyui一键图片转浮雕

一键生成图片浮雕效果&#xff1a;ComfyUI工作流指南 在图像处理和艺术创作领域&#xff0c;生成浮雕效果的图片是一种既独特又吸引人的表现手法。使用ComfyUI工作流&#xff0c;可以一键生成灰度图、深度图和浮雕图&#xff0c;大大简化了复杂的图像处理过程。本文将详细介绍…

嵌入式C语言之结构体封装函数

嵌入式C语言之结构体封装函数 Chapter1 嵌入式C语言之结构体封装函数结构体封装函数的作用结构体封装函数的应用结构体封装函数的好处举例1举例2举例3 Chapter1 嵌入式C语言之结构体封装函数 原文链接&#xff1a;https://blog.csdn.net/qq_43416206/article/details/13140531…

spring:Springboot3使用模版引擎thymeleaf

文章目录 介绍语法1、文本替换2、属性替换3、条件判断4. 列表循环5. 表单处理 基本示例视图解析机制视图解析器的默认配置为什么用Controller可以&#xff0c;用RestController就只是返回字符串 介绍 Thymeleaf 是一个现代的服务器端 Java 模板引擎&#xff0c;用于在服务器端…

掌握这几款在线音频剪辑工具,轻松成为音频处理达人!

在数字时代&#xff0c;音频剪辑已经成为一项必备技能。无论是制作短视频、播客&#xff0c;还是进行音乐创作&#xff0c;一款好用的音频剪辑工具都能助你事半功倍。今天&#xff0c;我们就来为大家推荐几款实用的在线音频剪辑工具&#xff0c;让你轻松成为音频处理达人&#…

救命!后悔没早点读,自学Python,这本书永远的神,经典又好懂!

这是一本对新手来说很友好的入门书&#xff0c;这本是今年才出的新版&#xff0c;之前的两个版本在某瓣都是9分以上了。专为初学者设计&#xff0c;同时也适合有编程经验的读者。该书由Eric Matthes编写&#xff0c;内容涵盖Python基础语法、编程概念以及丰富的实践项目。 全书…

【工具变量】文明城市评选DID(2000-2023年)

数据简介&#xff1a; 随着城市化的不断推进和全球城际竞争的日益激烈&#xff0c;城市品牌成为争夺优质资源、推动城市可持续发展的重要战略工具。通过关注城市品牌建设&#xff0c;不仅可以刺激本地企业家更多地进行创新活动&#xff0c;为企业家创新活动提供更好的营商环境…

自回溯天线:实现波束自动跟踪的智能天线系统

自回溯天线:实现波束自动跟踪的智能天线系统 1. 引言 自回溯天线是一种能够自动将接收到的信号发射回信号源方向的智能天线系统。它基于相位共轭原理,无需复杂的信号处理和控制系统,就能实现波束的自动跟踪。 自回溯天线技术依靠纯模拟方式实现&#xff0c;通过共轭模块对入射…

vue3中如何更改当前类的文件名称

首先&#xff0c;使用script指定文件名称 <template><div class"person"><h2>姓名&#xff1a;{{ name }}</h2><h2>年龄&#xff1a;{{ age }}</h2><button click"showTel">查看联系方式</button><bu…