Android实验十——使用SQLite数据库存储数据
一、实验要求和目的
- 掌握SQLite数据库的操作方法;
- 掌握SQLiteOpenHelper类的使用。
- 掌握Lsitview的使用。
二、实验环境
- 部署有Android Studio和Android SDK的主机;
- 建议在机房的HelloWorld例子上完成。
三、上机操作参考步骤
1、 实现如下简单的通讯录,主Acticity如图1所示。
图1
在图1中,点击“新增联系人”按钮后,启动图2所示的Acticity:
图2
在图2中用户输入姓名电话后,点击“保存”按钮,需要把姓名电话写入数据库表,然后关闭该Activity。主Activity重新回到运行状态,并刷新ListView显示出新增的联系人,如图3所示。
图3
在图1中,在文本框中输入要查找的联系人姓名或电话后,点击“查找”按钮,能够显示查找到的联系人的信息,如图4、图5所示。
图4 图5
这个实验还挺简单的,主要是老师把代码都给出来了🤡...
布局文件
activity_main.xml
<?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">
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="新增联系人"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_find"
android:layout_width="250dp"
android:layout_height="44dp"
android:layout_marginStart="20dp"
android:layout_marginTop="24dp"
android:hint="请输入姓名或电话"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_add" />
<Button
android:id="@+id/btn_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="查找"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/et_find"
app:layout_constraintTop_toBottomOf="@+id/btn_add" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="92dp"
android:layout_marginTop="28dp"
android:text="姓名"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_find" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="92dp"
android:layout_marginTop="28dp"
android:text="电话号码"
android:textSize="20dp"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/et_find" />
<ListView
android:id="@+id/lv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_add.xml
<?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=".AddActivity">
<EditText
android:id="@+id/et_name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_margin="80dp"
android:layout_marginTop="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_phone"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_name" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="88dp"
android:text="姓名:"
android:textSize="20dp"
app:layout_constraintEnd_toStartOf="@+id/et_name"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="电话:"
android:textSize="20dp"
app:layout_constraintEnd_toStartOf="@+id/et_phone"
app:layout_constraintTop_toBottomOf="@+id/et_name" />
<Button
android:id="@+id/btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="保存"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_phone" />
</androidx.constraintlayout.widget.ConstraintLayout>
cell.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[姓名]"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_Phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[电话号码]"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
java代码
MainActivity.java
package com.example.test10;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends AppCompatActivity {
private DbOpenHelper dbHelper = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 创建或打开数据库(此处需要使用绝对路径)
dbHelper = new DbOpenHelper(this, this.getFilesDir().toString() + "/contacts.db3", 1);
Button btn_add = findViewById(R.id.btn_add);
//为“新增联系人”按钮绑定监听器
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AddActivity.class);
startActivity(intent);
}
});
Button btn_find = findViewById(R.id.btn_find);
//为“查找”按钮绑定监听器
btn_find.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText et_find = findViewById(R.id.et_find);
String nameOrPhoneStr = et_find.getText().toString();
//执行查询操作
Cursor cursor = dbHelper.getReadableDatabase().rawQuery(
"select * from contact where name ==? or phone==?",
new String[]{nameOrPhoneStr, nameOrPhoneStr});
//显示查询结果
inflateList(cursor);
}
});
}
//当前Activity从停止状态再次回到运行状态时,onStart()函数会被回调
@Override
protected void onStart() {
super.onStart();
Cursor cursor = dbHelper.getReadableDatabase()
.rawQuery("select * from contact", null);
inflateList(cursor);
}
//把查询结果cursor,填充显示到ListView控件中
private void inflateList(Cursor cursor) {
// 填充SimpleCursorAdapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
MainActivity.this, R.layout.cell, cursor, new String[]{"name", "phone"},
new int[]{R.id.tv_Name, R.id.tv_Phone}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER
);
// 显示数据
ListView lv_show = findViewById(R.id.lv_show);
lv_show.setAdapter(adapter);
}
//当前Activity销毁前onDestroy()函数会被回调
@Override
protected void onDestroy() {
super.onDestroy();
// 退出程序时关闭MyDatabaseHelper里的SQLiteDatabase
dbHelper.close();
}
}
AddActivity.java
package com.example.test10;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AddActivity extends AppCompatActivity {
private DbOpenHelper dbHelper = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
//获取SQLiteOpenHelper对象
dbHelper = new DbOpenHelper(this, this.getFilesDir().toString() + "/contacts.db3", 1);
Button btn_save = findViewById(R.id.btn_save);
//为“保存”按钮绑定监听器
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText et_name = findViewById(R.id.et_name);
EditText et_phone = findViewById(R.id.et_phone);
String nameStr = et_name.getText().toString();
String phoneStr = et_phone.getText().toString();
// 数据库表执行插入语句
dbHelper.getReadableDatabase().execSQL("insert into contact values(null , ? , ?)",
new String[]{nameStr, phoneStr});
finish();
}
});
}
}