当前位置: 首页 > article >正文

网络资源模板--Android Studio 实现绿豆通讯录

目录

一、项目演示

二、项目测试环境

三、项目关键源码

四、项目源码 


一、项目演示

网络资源模板--基于Android studio 实现的通讯录

二、项目测试环境

三、项目关键源码

  switch (v.getId()) {
            case R.id.btn_add: //添加数据
                name = mEtName.getText().toString();
                phone = mEtPhone.getText().toString();
                db = myHelper.getWritableDatabase();//获取可读写SQLiteDatabse对象
                values = new ContentValues();       // 创建ContentValues对象
                values.put("name", name);           // 将数据添加到ContentValues对象
                values.put("phone", phone);
                db.insert("information", null, values);
                Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show();
                db.close();
                break;
            case R.id.btn_query: //查询数据
                db = myHelper.getReadableDatabase();
                Cursor cursor = db.query("information", null, null, null, null,
                        null, null);
                if (cursor.getCount() == 0) {
                    mTvShow.setText("");
                    Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
                } else {
                    cursor.moveToFirst();
                    mTvShow.setText("Name :  " + cursor.getString(1) +
                            "  ;Tel :  " + cursor.getString(2));
                }
                while (cursor.moveToNext()) {
                    mTvShow.append("\n" + "Name :  " + cursor.getString(1) +
                            "  ;Tel :  " + cursor.getString(2));
                }
                cursor.close();
                db.close();
                break;

这段代码是一个 Android 应用中的按钮点击事件处理,其中涉及了 SQLite 数据库的操作。具体功能如下:

  1. 添加数据(btn_add 按钮点击事件)

    • 从用户输入框(mEtName 和 mEtPhone)获取数据,分别存储在 name 和 phone 变量中。
    • 获取可写的 SQLite 数据库对象(db)。
    • 创建一个 ContentValues 对象,用于存储要插入的数据。
    • 将 name 和 phone 存入 ContentValues 中,并通过 db.insert() 方法将其插入到数据库的 information 表中。
    • 插入成功后,显示提示信息 "信息已添加"。
    • 关闭数据库连接。
  2. 查询数据(btn_query 按钮点击事件)

    • 获取可读的 SQLite 数据库对象(db)。
    • 使用 db.query() 查询 information 表中的所有数据。
    • 如果查询结果为空(即没有数据),清空显示文本(mTvShow.setText("")),并显示提示 "没有数据"。
    • 如果有数据,首先显示查询结果中的第一行数据(姓名和电话)。
    • 通过 while (cursor.moveToNext()) 循环遍历查询到的所有数据,并将每条记录的姓名和电话附加到 mTvShow 文本视图中。
    • 查询完成后,关闭游标(cursor.close())和数据库连接。

 对应的xml代码

<?xml version="1.0" encoding="utf-8"?>
<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:background="@drawable/bg"
    android:paddingLeft="16dp"
    android:paddingTop="16dp"
    android:paddingRight="16dp"
    android:paddingBottom="16dp"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/ll_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/ll_phone"
        android:layout_alignStart="@+id/ll_btn"
        android:layout_alignLeft="@+id/ll_btn">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓  名 :"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入姓名"
            android:textSize="16sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/ll_btn"
        android:layout_alignStart="@+id/ll_name"
        android:layout_alignLeft="@+id/ll_name"
        android:layout_marginBottom="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电  话 :"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/et_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入手机号码"
            android:textSize="16sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true">

        <Button
            android:id="@+id/btn_add"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:background="#B9B9FF"
            android:text="添加"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btn_query"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:background="#DCB5FF"
            android:text="查询"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btn_update"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:background="#E6CAFF"
            android:text="修改"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#ACD6FF"
            android:text="删除"
            android:textSize="18sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll_btn"
        android:layout_marginTop="25dp"
        android:textSize="20sp" />
</RelativeLayout>

四、项目源码 

👇👇👇👇👇快捷方式👇👇👇👇👇


http://www.kler.cn/a/427969.html

相关文章:

  • 【力扣】49.字母异位词分组
  • 反向代理模块b
  • 步入响应式编程篇(三)之spring webFlux与R2DBC
  • thinkphp6+swoole使用rabbitMq队列
  • LMI Gocator GO_SDK VS2019引用配置
  • 20个整流电路及仿真实验汇总
  • 【springboot】 多数据源实现
  • 塑胶模具基本结构及塑胶成型原理
  • ubuntu 使用USB转TTL线连接树莓派4B
  • 【Android】ARouter源码解析
  • Python 信息科技赛课区一等奖教案(语音合成技术)
  • 【Elasticsearch】初始化默认字段及分词
  • Python中的数据可视化实战
  • spring-boot-starter-validation校验启动器简述
  • 基于SpringBoot校园台球厅人员与设备管理系统设计与实现
  • JavaScript Server-Sent Events (SSE) 教程
  • 国产GPU中,VLLM0.5.0发布Qwen2.5-14B-Instruct-GPTQ-Int8模型,请求返回结果乱码
  • 宝塔控制面板phpMyadmin打开卡主加载中状态
  • Spring Boot教程之二十一:文件处理
  • 样品前处理工作站自动化操作
  • JavaWeb学习(4)(四大域、HttpSession原理(面试)、SessionAPI、Session实现验证码功能)
  • lua 一个简单的table变量序列化和日志写入函数
  • gitee常见命令
  • 日本IT|AWS技术方向都需要做哪些工作呢?
  • 认证插件介绍
  • 【Pytorch】学习第一弹——张量数据类型、创建张量、索引与切片、维度变换、Broadcasting、合并与分割、数学运算