Android基于SQLite的通信录实现

1.知识点

Android系统中集成了SQLite数据库,并且为数据库的操作提供了相关的类和方法,便于没有数据库开发经验的开发者编写程序。另外,Android平台中得用ContentProvider机制来实现跨应用程序数据共享。一个应用程序可以通过ContentProvider来发布自己的数据,其他的应用程序可以通过ContentResolver来获取共享数据。

2.功能

  • 实现基于SQLite数据库通信录应用,运行结果视频所示。通过单击增加图标打开添加通信录界面,通过单击通信录中的各条信息可删除选中项。
  • 增加批量删除功能。
    在这里插入图片描述

3.实现

  • (1)建立数据库操作类DatabaseHelper,参考代码如下:
public class DatabaseHelper extends SQLiteOpenHelper {
   
    private static final String DB_NAME="MyRelation.db";
    private static final String TABLE_NAME="relation";
    private static final String CREATE_TABLE="create table relation(" +
            "_id integer primary key autoincrement,name text,tel text,groupName text)";
    private SQLiteDatabase db;
    public DatabaseHelper(Context context)
    {
   
        super(context,DB_NAME,null,2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
   
        this.db=db;
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   }
    public void insert(ContentValues values){
   
        SQLiteDatabase db=getWritableDatabase();
        db.insert(TABLE_NAME,null,values);
        db.close();
    }
    public void del(int id){
   
        if(null==db)
            db=getWritableDatabase();
        db.delete(TABLE_NAME,"_id=?",new String[]{
   String.valueOf(id)});
    }
    public Cursor query(){
   
        SQLiteDatabase db=getWritableDatabase();
        Cursor cursor=db.query(TABLE_NAME,null,null,null,null,null,null);
        return cursor;
    }
    public void close()
    {
   
        if(db!=null)
            db.close();
    }

}
  • (2)创建主布局,参考步骤如下:
    ①使用约束布局
    ②添加文本框,并显示“通信录”,id为title
    ③添加列表框,id为listView
    ④添加图片按钮组件,并设置响应方法为“myClick”
    ⑤添加编辑按钮(点击后进入批量删除,Checkbox显示),确定按钮(批量删除模式时候显示,其他模式隐藏)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="通讯录"
        android:textSize="30sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/title"/>

    <Button
        android:id="@+id/sure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:visibility="invisible"
        android:text="确定"/>

    <Button
        android:id="@+id/write"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:text="编辑"/>

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="50dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/addperson"
        android:layout_marginBottom="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:id="@+id/myClick"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
  • (3)创建列表视图布局文件relationlist.xml,参考步骤如下:
    ①使用水平线性布局管理器
    ②添加id文本框,id为_id,设置为不显示
    ③添加姓名文本框,id为name
    ④添加电话文本框,id为tel
    ⑤添加联系人群组文本框,id为group
    ⑥添加用于多选的CheckBox,id为check
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值