网络资源模板--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>

四、项目源码

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

相关推荐
活宝小娜几秒前
新增和编辑共用弹窗模板
开发语言·前端·javascript·vue.js
撩得Android一次心动31 分钟前
Android 项目:画图白板APP开发(一)——曲线优化、颜色、粗细、透明度
android
IT毕设实战小研36 分钟前
Java毕业设计选题推荐 |基于SpringBoot的健身爱好线上互动与打卡社交平台系统 互动打卡小程序系统
java·开发语言·vue.js·spring boot·vue·毕业设计·课程设计
小离a_a2 小时前
根据图片远程地址复制图片内容,可以在富文本、word等文本里粘贴
开发语言·前端·javascript
宇寒风暖5 小时前
@(AJAX)
前端·javascript·笔记·学习·ajax
月夜风雨磊9 小时前
Android NDK从r10c版本到r29版本的下载链接
android·gitee·android ndk
louisgeek9 小时前
Android MIUI 开启 Google Play 服务
android
Giser探索家9 小时前
低空智航平台技术架构深度解析:如何用AI +空域网格破解黑飞与安全管控难题
大数据·服务器·前端·数据库·人工智能·安全·架构
码界筑梦坊10 小时前
135-基于Spark的抖音数据分析热度预测系统
大数据·python·数据分析·spark·毕业设计·echarts
独行soc10 小时前
2025年大模型安全岗的面试汇总(题目+回答)
android·人工智能·安全·面试·职场和发展·渗透测试