Android實(shí)現(xiàn)通訊錄功能
本文實(shí)例為大家分享了Android通訊錄案例,供大家參考,具體內(nèi)容如下
實(shí)戰(zhàn)演練——通訊錄
1、功能描述:通過SQLite實(shí)現(xiàn)數(shù)據(jù)庫的增刪改查
2、技術(shù)要點(diǎn):SQLite的基本操作
3、實(shí)現(xiàn)步驟:
① 創(chuàng)建一個(gè)類繼承SQLiteOpenHelper
② 重寫父類構(gòu)造方法、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); } //當(dāng)數(shù)據(jù)庫第一次創(chuàng)建的時(shí)候執(zhí)行 @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, "沒有數(shù)據(jù)", 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, "信息已經(jīng)刪除", 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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:layout_width="160dp" android:layout_height="120dp" android:layout_marginTop="50dp" android:layout_marginLeft="20dp" android:src="@drawable/expression"></ImageView> <ImageView android:layout_width="160dp" android:layout_height="120dp" android:layout_marginTop="50dp" android:layout_marginLeft="20dp" android:src="@drawable/text"></ImageView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="20dp" android:paddingHorizontal="20dp" > <TextView 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" /> <EditText android:id="@+id/name" android:layout_width="0dp" android:layout_weight="3" android:layout_height="wrap_content" android:hint="請(qǐng)輸入姓名" android:textSize="22sp" ></EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="20dp" android:paddingHorizontal="20dp" > <TextView 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" /> <EditText android:id="@+id/phone" android:layout_width="0dp" android:layout_weight="3" android:layout_height="wrap_content" android:hint="請(qǐng)輸入手機(jī)號(hào)碼" android:textSize="22sp" ></EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="20dp" android:paddingHorizontal="20dp" > <Button android:id="@+id/insert" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="增加" android:textSize="26sp" ></Button> <Button android:id="@+id/select" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="查詢" android:textSize="26sp" ></Button> <Button android:id="@+id/update" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="修改" android:textSize="26sp" ></Button> <Button android:id="@+id/delete" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="刪除" android:textSize="26sp" ></Button> </LinearLayout> <TextView 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" ></TextView> </LinearLayout> </RelativeLayout>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用adb命令向Android模擬器中導(dǎo)入通訊錄聯(lián)系人的方法
- Android獲取手機(jī)通訊錄、sim卡聯(lián)系人及調(diào)用撥號(hào)界面方法
- Android通訊錄開發(fā)之刪除功能的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)通訊錄效果——獲取手機(jī)號(hào)碼和姓名
- Android個(gè)人手機(jī)通訊錄開發(fā)詳解
- Android實(shí)現(xiàn)仿通訊錄側(cè)邊欄滑動(dòng)SiderBar效果代碼
- Android破解微信獲取聊天記錄和通訊錄信息(靜態(tài)方式)
- android仿微信通訊錄搜索示例(匹配拼音,字母,索引位置)
- Android仿微信通訊錄打造帶懸停頭部的分組列表(上)
- Android讀取手機(jī)通訊錄聯(lián)系人到自己項(xiàng)目
相關(guān)文章
Android利用ObjectAnimator實(shí)現(xiàn)ArcMenu
這篇文章主要為大家詳細(xì)介紹了Android利用ObjectAnimator實(shí)現(xiàn)ArcMenu的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07Android PhotoView使用步驟實(shí)例詳解
這篇文章主要介紹了Android PhotoView使用步驟實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06loadavg數(shù)據(jù)異常引發(fā)問題起源分析
這篇文章主要為大家介紹了loadavg數(shù)據(jù)異常引發(fā)問題起源分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android獲取常用輔助方法(獲取屏幕高度、寬度、密度、通知欄高度、截圖)
我們需要獲取Android手機(jī)或Pad的屏幕的物理尺寸,以便于界面的設(shè)計(jì)或是其他功能的實(shí)現(xiàn)。下面就分享一下Android中常用的一些輔助方法2016-02-02Android 防止多次重復(fù)點(diǎn)擊的三種方法的示例
本篇文章主要介紹了Android 防止多次重復(fù)點(diǎn)擊的三種方法的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03Android仿微信和QQ多圖合并框架(類似群頭像)的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Android仿微信和QQ多圖合并框架的相關(guān)資料,其實(shí)就是我們平時(shí)所見的群聊頭像,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12Android網(wǎng)絡(luò)編程之UDP通信模型實(shí)例
這篇文章主要介紹了Android網(wǎng)絡(luò)編程之UDP通信模型實(shí)例,本文給出了服務(wù)端代碼和客戶端代碼,需要的朋友可以參考下2014-10-10android RecyclerView的一些優(yōu)化點(diǎn)介紹
大家好,本篇文章主要講的是android RecyclerView的一些優(yōu)化點(diǎn)介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12