本文实例为大家分享了Android通讯录案例,供大家参考,具体内容如下

实战演练——通讯录

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);

}

//当数据库第一次创建的时候执行

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT ,name VARCHAR(20),phone VARCHAR(20))");

}

@Override

public 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;

@Override

protected 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);

}

@Override

public 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

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">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

android:layout_width="160dp"

android:layout_height="120dp"

android:layout_marginTop="50dp"

android:layout_marginLeft="20dp"

android:src="@drawable/expression">

android:layout_width="160dp"

android:layout_height="120dp"

android:layout_marginTop="50dp"

android:layout_marginLeft="20dp"

android:src="@drawable/text">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:layout_marginTop="20dp"

android:paddingHorizontal="20dp"

>

android: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" />

android:id="@+id/name"

android:layout_width="0dp"

android:layout_weight="3"

android:layout_height="wrap_content"

android:hint="请输入姓名"

android:textSize="22sp"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:layout_marginTop="20dp"

android:paddingHorizontal="20dp"

>

android: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" />

android:id="@+id/phone"

android:layout_width="0dp"

android:layout_weight="3"

android:layout_height="wrap_content"

android:hint="请输入手机号码"

android:textSize="22sp"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:layout_marginTop="20dp"

android:paddingHorizontal="20dp"

>

android:id="@+id/insert"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:text="增加"

android:textSize="26sp"

>

android:id="@+id/select"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:text="查询"

android:textSize="26sp"

>

android:id="@+id/update"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:text="修改"

android:textSize="26sp"

>

android:id="@+id/delete"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:text="删除"

android:textSize="26sp"

>

android: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"

>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

android通讯录完整功能实现,Android实现通讯录功能相关推荐

  1. android通讯录开发(粗糙实现微信通讯录的功能)

    主要实现功能: 一.实现名单的中英文混合排序,按照联系人名单的首字母进行分类. 二.实现右侧的字母导航功能,根据字母定位相应的名单组,方便查找. 三.实现搜索功能(1)输入字母进行搜索时,按照精确查找 ...

  2. android 更新通讯录,搜狗号码通Android4.4更新 新增通讯录备份功能

    在社交软件被高度利用的今天,手机通讯录仍然是一个不可替代的功能.当我们更换新手机或者重新刷机.恢复出厂设置后,都需要进行一项重要操作--备份联系人.然而很多用户由于不熟悉备份软件,往往研究半天才能备份 ...

  3. android通讯录上传服务器,Android 实现读取通讯录并上传服务器

    关键技术 - 内容解析者Resolver - ListView - Socket网络编程 权限申请 //需要在Manifest.xml文件中申请权限 布局: xmlns:app="http: ...

  4. 史上功能完整强大的android下载管理器

    废话不说,有图为证: 功能完整强大的android下载管理器, 支持多任务管理,断点续传,暂停下载, 下载任务图标设置,状态栏通知(进度条),媲美国内知名软件市场hiapk,360手机助手等内置的下载 ...

  5. android有什么作用,Android 7.0有什么功能 Android N完整功能参数介绍

    类型:系统工具大小:1000M语言:中文 评分:10.0 标签: 立即下载 Android 7.0是安卓最近正在更新的一个版本,而其中也是有着很多的特性,在Android N的逐渐的曝光中,我们也是可 ...

  6. android手机通讯录格式转换,手机通讯录小技巧,安卓手机通讯录转iPhone并不难,换机必学...

    原标题:手机通讯录小技巧,安卓手机通讯录转iPhone并不难,换机必学 今年618年中大促销,国内的电商巨头们分分大减价,就连号称保价的苹果手机,过万iPhone XS MAX都跌入八千户里.相信很多 ...

  7. android 通讯录备份 导入苹果手机,安卓手机通讯录怎么导入苹果手机? 手机通讯录怎么恢复导出...

    安卓手机通讯录怎么导入苹果手机?手机通讯录怎么恢复导出?对于手机数据被删的情况,真的是来去匆匆,不带走一点遗憾.轻轻的触碰联系人的删除键就可以把联系人联系方式进行清空了,那么应该如何恢复呢? 现在如此 ...

  8. android手机如何到导出电话号码,手机通讯录怎么导入到新手机?这招太给力了!...

    手机通讯录怎么导入到新手机?这招太给力了! 2020年01月01日 14:41作者:网络编辑:宏伟 分享 手机通讯录怎么导入到新手机?很多人在第一次接触iPhone的时候,往往会遇到老手机通讯录联系人 ...

  9. Android系统完整的权限列表

    访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permiss ...

最新文章

  1. 分布式框架-日志系统思路及实现
  2. efcore mysql autofac_Asp.NetCore3.1版本的CodeFirst与经典的三层架构与AutoFac批量注入
  3. Redis主从同步和持久化
  4. 技术人生——解决问题的规律
  5. 英雄联盟祖安服务器位置,LOL“4区版英雄联盟”:城区有2个,郊区有2个,山区有1个...
  6. jstack(查看线程)、jmap(查看内存)和jstat(性能分析)命令
  7. 《Improving Langugage Understanding by Generative Pre-Tranining》 —— GPT
  8. 熊猫DataFrame from_dict()–字典到DataFrame
  9. 360浏览器通过访问插件管理界面启用flash实例演示,360浏览器启用Adobe Flash Player方法
  10. java dms项目流程_Xinco DMS
  11. Android 第三方轮播图控件ConvenientBanner:通用的广告栏控件
  12. NOIP2021 T3 方差
  13. 城乡规划编制资质很多地区已经开通新办了,那你知道怎么办吗?
  14. FileZilla FTP服务器源代码分析
  15. Hadoop修改slaves的主机名,所要修改的文件
  16. 虚函数、抽象函数、抽象类、接口
  17. 牢记卖股票的四大纪律十项注意
  18. 量化:通过ta-lib计算MA5指标
  19. Otter 双向同步mysql
  20. 关于Java代码中一个方法代码超出65535字节

热门文章

  1. 12月9日科技资讯|苹果三星手机被诉辐射超标;淘集集启动破产清算;Apache Flink 1.9.1 发布 |
  2. python爬取都挺好影视评论,看看大家的共鸣度有多强?
  3. ColumnTransformer()函数
  4. oracle查找相似字段,两表相似字段查询,如何查询
  5. TP-Admin 一个拥有站群功能的多功能CMS基础系统
  6. 大数据时代:架构师该具备什么?
  7. Attentional Factorization Machine(AFM)复现笔记
  8. windows下mysql免安装配置
  9. 微信支付服务商,可视化进件特约商户
  10. 弹性与智能—下一代移动网络系统(RINGS)