实战演练——通讯录

  1. 功能描述:通过SQLite实现数据库的增删改查

  2. 技术要点:SQLite的基本操作

  3. 实现步骤:
    ① 创建一个类继承SQLiteOpenHelper
    ② 重写父类构造方法、onCreate()、onUpgrade()
    ③ 增删改查

  4. 效果图

  5. 案例代码
    MyHelper.java

package com.example.sqlite;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;import androidx.annotation.Nullable;public class MyHelper extends SQLiteOpenHelper {public MyHelper(@Nullable Context context) {super(context, "test.db", null, 1);}//当数据库第一次创建的时候执行@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT ,name VARCHAR(20),phone VARCHAR(20))");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}

MainActivity.java

package com.example.sqlite;import androidx.appcompat.app.AppCompatActivity;import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private TextView name;private TextView phone;private Button btnAdd;private Button btnDel;private Button btnUqd;private Button btnSel;private String uPhone;private String uName;private MyHelper myHelper;private SQLiteDatabase db;private TextView show;private ContentValues contentValues;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myHelper = new MyHelper(this);init();}private void init() {show = findViewById(R.id.show);name = findViewById(R.id.name);phone = findViewById(R.id.phone);btnAdd = findViewById(R.id.insert);btnDel = findViewById(R.id.delete);btnUqd = findViewById(R.id.update);btnSel = findViewById(R.id.select);btnAdd.setOnClickListener(this);btnDel.setOnClickListener(this);btnUqd.setOnClickListener(this);btnSel.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.select:db = myHelper.getReadableDatabase();Cursor cursor = db.query("information", null, null, null, null, null, null);if (cursor.getCount() == 0) {Toast.makeText(this, "没有数据", Toast.LENGTH_LONG).show();} else {cursor.moveToFirst();show.setText("Name:" + cursor.getString(1) + "Tel:" + cursor.getString(2));}while (cursor.moveToNext()) {show.append("\n" + "Name" + cursor.getString(1) + "Tel" + cursor.getString(2));}cursor.close();db.close();break;case R.id.insert:uName = name.getText().toString();uPhone = phone.getText().toString();db = myHelper.getReadableDatabase();contentValues = new ContentValues();contentValues.put("name", uName);contentValues.put("phone", uPhone);db.insert("information", null, contentValues);db.close();break;case R.id.update:db = myHelper.getReadableDatabase();contentValues = new ContentValues();contentValues.put("phone", uPhone = phone.getText().toString());db.update("information", contentValues, "name=?", new String[]{name.getText().toString()});db.close();break;case R.id.delete:db = myHelper.getReadableDatabase();db.delete("information", null, null);Toast.makeText(this, "信息已经删除", Toast.LENGTH_LONG).show();show.setText("");db.close();break;}}
}

activity_main.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"tools:context=".MainActivity"android:background="@drawable/background"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><ImageViewandroid:layout_width="160dp"android:layout_height="120dp"android:layout_marginTop="50dp"android:layout_marginLeft="20dp"android:src="@drawable/expression"></ImageView><ImageViewandroid:layout_width="160dp"android:layout_height="120dp"android:layout_marginTop="50dp"android:layout_marginLeft="20dp"android:src="@drawable/text"></ImageView></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="20dp"android:paddingHorizontal="20dp"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="姓 名 :"android:textSize="26sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><EditTextandroid:id="@+id/name"android:layout_width="0dp"android:layout_weight="3"android:layout_height="wrap_content"android:hint="请输入姓名"android:textSize="22sp"></EditText></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="20dp"android:paddingHorizontal="20dp"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="电 话 :"android:textSize="26sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /><EditTextandroid:id="@+id/phone"android:layout_width="0dp"android:layout_weight="3"android:layout_height="wrap_content"android:hint="请输入手机号码"android:textSize="22sp"></EditText></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="20dp"android:paddingHorizontal="20dp"><Buttonandroid:id="@+id/insert"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:text="增加"android:textSize="26sp"></Button><Buttonandroid:id="@+id/select"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:text="查询"android:textSize="26sp"></Button><Buttonandroid:id="@+id/update"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:text="修改"android:textSize="26sp"></Button><Buttonandroid:id="@+id/delete"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:text="删除"android:textSize="26sp"></Button></LinearLayout><TextViewandroid:id="@+id/show"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:textSize="18sp"android:background="#80ffffff"android:layout_marginHorizontal="20dp"></TextView></LinearLayout>
</RelativeLayout>

Android通讯录案例相关推荐

  1. android通讯录完整功能实现,Android实现通讯录功能

    本文实例为大家分享了Android通讯录案例,供大家参考,具体内容如下 实战演练--通讯录 1.功能描述:通过SQLite实现数据库的增删改查 2.技术要点:SQLite的基本操作 3.实现步骤: ① ...

  2. 《Android 应用案例开发大全(第二版)》——2.6节绘制相关类

    本节书摘来自异步社区<Android 应用案例开发大全(第二版)>一书中的第2章,第2.6节绘制相关类 ,作者 吴亚峰 , 于复兴 , 杜化美,更多章节内容可以访问云栖社区"异步 ...

  3. Jquery Mobile设计Android通讯录第二章

    本文是jQuery Mobile设计Android通讯录系统教程的第二篇,在上一篇教程中(http://publish.itpub.net/a2011/0517/1191/000001191561.s ...

  4. android通讯录管理(获取联系人,通话记录,短信消息),Android通讯录管理(获取联系人、通话记录、短信消息)(二)...

    Android通讯录管理(获取联系人.通话记录.短信消息)(二) 前言:上一篇博客介绍的是获取联系人的实现,本篇博客将介绍通话记录的实现. 界面布局: /Contact_Demo/res/layout ...

  5. 《Android 应用案例开发大全(第二版)》——6.1节Android系统的信使:Intent

    本节书摘来自异步社区<Android 应用案例开发大全(第二版)>一书中的第6章,第6.1节Android系统的信使:Intent ,作者李宁,更多章节内容可以访问云栖社区"异步 ...

  6. 《Android 应用案例开发大全(第二版)》——导读

    本节书摘来自异步社区<Android 应用案例开发大全(第二版)>一书中的目录 ,作者 吴亚峰 , 于复兴 , 杜化美,更多章节内容可以访问云栖社区"异步社区"公众号查 ...

  7. Android 系统性能优化(43)---Android OOM案例分析

    Android OOM案例分析 在Android(Java)开发中,基本都会遇到java.lang.OutOfMemoryError(本文简称OOM),这种错误解决起来相对于一般的Exception或 ...

  8. android通讯录备份恢复代码逻辑

    android通讯录备份恢复 新增和修改 目前用的是新增,云端判断返回的手机号是否为空 /*** 向手机中录入联系人信息** @param contactListBean 要录入的联系人信息*/pub ...

  9. 《Android开发案例驱动教程》

    <Android开发案例驱动教程> 作者:关东升,赵志荣 Java或C++程序员转变成为Android程序员 采用案例驱动模式展开讲解知识点,即介绍案例->案例涉及技术->展开 ...

最新文章

  1. Ecol. Lett.:写给实践生态学家的β多样性分析指南 | 朝花夕拾
  2. 使用Nodejs创建基本的网站 Microblog--《Node.js开发指南》 1
  3. 2019.6.18 校内测试 分析+题解
  4. 机器学习相关从业者如何兼顾理论与工程能力
  5. linux第五周微职位
  6. 三种GDB类型的转换后字段类型的变化
  7. oracle 韩思捷,oracle
  8. 用python写一个地铁线路图_python制作一线城市地铁运行动态图
  9. 2020-12-3background-color对div元素不起作用
  10. 学习汇报9.26-9.28
  11. ps制作台式计算机图标,ps制作计算机图标
  12. 052试题 86 - crosscheck 命令及expried
  13. h5拍照添加水印上传
  14. linux桌面 任务栏,状态栏消失恢复
  15. 什么是堡垒机?堡垒机的作用?
  16. npm install 报node-sass错误
  17. ecshop支持mysql_ecshop安装不支持MySQL
  18. JAVA incept_关于Inception默认配置的一个坑
  19. 【渝粤教育】电大中专计算机职业素养_1作业 题库
  20. BeanDefinition用法

热门文章

  1. ASP.NET - ASP.NET 服务器控件验证类型 - RangeValidator 类
  2. 飞桨文心大模型挑战高考作文:平均1秒生成1篇,水平超75%考生
  3. 欧氏距离,曼哈顿距离,闵可夫斯基距离,马氏距离,汉明距离
  4. PHP祛斑,激光祛斑后悔死了
  5. 【数据结构】—— Java实现双向链表
  6. Android Activity底层启动过程分析
  7. 特征提取(Feature Detect)、特征描述(Feature Descriptor)、特征匹配(Feature Match)
  8. invalid json response body at https://registry.npmjs.org/vue reason: Unexpected end of JSON input
  9. fgvc-aircraft-2013b飞机细粒度数据训练集和测试集划分python代码
  10. Perl 数组应用详解(push, pop, shift, unshift)