Android实现通讯录功能

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

实战演练——通讯录

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

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

3、实现步骤:

① 创建一个类继承SQLiteOpenHelper
② 重写父类构造方法、onCreate()、onUpgrade()
③ 增删改查

4、效果图

Android实现通讯录功能_第1张图片

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



  
    

      
      
    
    
      
      

    
    
      
      

    
    
      
      
      
      
    
    
  

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

你可能感兴趣的:(Android实现通讯录功能)