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

目录

一、项目演示

二、项目测试环境

三、项目关键源码

四、项目源码


一、项目演示

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

二、项目测试环境

三、项目关键源码

java 复制代码
  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 按钮点击事件)

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

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

对应的xml代码

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>

四、项目源码

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

相关推荐
树上有只程序猿1 分钟前
Claude 4提升码农生产力的5种高级方式
前端
casual_clover2 分钟前
Android 之 kotlin语言学习笔记三(Kotlin-Java 互操作)
android·java·kotlin
傻球2 分钟前
没想到干前端2年了还能用上高中物理运动学知识
前端·react.js·开源
咚咚咚ddd2 分钟前
前端组件:pc端通用新手引导组件最佳实践(React)
前端·react.js
Lazy_zheng3 分钟前
🚀 前端开发福音:用 json-server 快速搭建本地 Mock 数据服务
前端·javascript·vue.js
HJ_Coder3 分钟前
基于Proxyman的实时解密和预览方案
前端
Gixy4 分钟前
聊聊纯函数与不可变数据结构
前端·设计模式
ZzMemory4 分钟前
藏起来的JS(四) - GC(垃圾回收机制)
前端·javascript·面试
lsustc5 分钟前
让AI 帮我写一篇前端技术文章 一(Element Plus 主题编辑器)
前端
ChaselHi7 分钟前
G2Plot图表库——桑基图开发
前端