1 实验名称
备忘录
2 实验目的
掌握SQLite数据库的基本操作,实现备忘录基本功能。
3 实验源代码
布局文件代码:
(1)activity_main.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"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><TableLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TableRow><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:text="主题:"android:textSize="20sp" /><EditTextandroid:id="@+id/et_subject"android:layout_width="match_parent"android:layout_height="wrap_content"/></TableRow><TableRow><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:text="内容:"android:textSize="20sp" /><EditTextandroid:id="@+id/et_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:minLines="4"/></TableRow><TableRow><Buttonandroid:id="@+id/btn_chooseDate"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="选择时间"/><EditTextandroid:id="@+id/et_data"android:layout_width="200dp"android:layout_height="wrap_content"android:editable="false"/></TableRow></TableLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"><Buttonandroid:id="@+id/btn_add"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="添加"/><Buttonandroid:id="@+id/btn_query"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="查询"/></LinearLayout><LinearLayoutandroid:id="@+id/title"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewstyle="@style/TextView"android:layout_width="40dp"android:text="序号"android:textColor="#000000"/><TextViewstyle="@style/TextView"android:layout_width="50dp"android:text="主题"android:textColor="#000000"/><TextViewstyle="@style/TextView"android:layout_width="110dp"android:text="内容"android:textColor="#000000"/><TextViewstyle="@style/TextView"android:layout_width="100dp"android:text="时间"android:textColor="#000000"/></LinearLayout><ListViewandroid:id="@+id/lv_result"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>
(2)result.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><TextViewandroid:id="@+id/tv_num"android:layout_width="40dp"style="@style/TextView"android:text="序号"/><TextViewandroid:id="@+id/tv_subject"android:layout_width="50dp"style="@style/TextView"android:text="主题"/><TextViewandroid:id="@+id/tv_content"android:layout_width="40dp"style="@style/TextView"android:text="内容"/><TextViewandroid:id="@+id/tv_date"android:layout_width="40dp"style="@style/TextView"android:text="时间"/></LinearLayout>
Java代码:
(1)MainActivity
package com.example.memorandumtest;import android.app.DatePickerDialog;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;import java.util.Calendar;public class MainActivity extends AppCompatActivity {private EditText et_subject = null;private EditText et_content = null;private EditText et_date = null;private Button btn_Choosedate = null;private Button btn_add = null;private Button btn_query = null;private LinearLayout title = null;private ListView lv_result = null;//定义数据库工具类对象private MyDatabaseHelper myDatabaseHelper = null;//定义数据库对象private SQLiteDatabase sqLiteDatabase = null;private MyListener myListener = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_subject = findViewById(R.id.et_subject);et_content = findViewById(R.id.et_content);et_date = findViewById(R.id.et_data);btn_Choosedate = findViewById(R.id.btn_chooseDate);btn_add = findViewById(R.id.btn_add);btn_query = findViewById(R.id.btn_query);title = findViewById(R.id.title);lv_result = findViewById(R.id.lv_result);btn_Choosedate.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//创建一个日历的对象Calendar calendar = Calendar.getInstance();new DatePickerDialog(MainActivity.this,new DatePickerDialog.OnDateSetListener() {//日期选择监听器@Overridepublic void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {et_date.setText(year+"-"+(month+1)+"-"+dayOfMonth);}},calendar.get(Calendar.YEAR),//设置当前的年份calendar.get(Calendar.MONTH),//设置当前的月份calendar.get(Calendar.DAY_OF_MONTH)//设置当前的日期).show();}});//为添加和查询按钮绑定监听器对象myListener =new MyListener();btn_add.setOnClickListener(myListener);btn_query.setOnClickListener(myListener);}private class MyListener implements View.OnClickListener{@Overridepublic void onClick(View v) {//创建数据库工具类对象myDatabaseHelper = new MyDatabaseHelper(MainActivity.this,"memento.db",null, 1);//创建数据库对象sqLiteDatabase = myDatabaseHelper.getReadableDatabase();//把用户输入的数据取出来String subjectStr = et_subject.getText().toString().trim();String contentStr = et_content.getText().toString().trim();String dateStr = et_date.getText().toString().trim();if (v.getId() == R.id.btn_add) {//调用向数据库中添加记录的方法addMemento(sqLiteDatabase, subjectStr, contentStr, dateStr);} else if (v.getId() == R.id.btn_query) {//调用查询数据库中的记录的方法Cursor cursor = queryMemento(sqLiteDatabase, subjectStr, contentStr, dateStr);//将查询结果在列表中显示//创建一个简单游标适配器对象SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(MainActivity.this,R.layout.result,//列表中每一项的布局文件cursor,new String[]{"_id", "subject", "content", "date"},//数据表中的字段信息new int[]{R.id.tv_num, R.id.tv_subject, R.id.tv_content, R.id.tv_date});//让列表适用适配器对象lv_result.setAdapter(simpleCursorAdapter);}}}//向数据库添加记录的方法private void addMemento(SQLiteDatabase database,String subject,String content,String date){database.execSQL("insert into memento_tb values(null,?,?,?)",new String[]{subject,content,date});//清空数据方便用户下一次输入et_subject.setText("");et_content.setText("");et_date.setText("");}//查询数据库记录的方法private Cursor queryMemento(SQLiteDatabase sqLiteDatabase,String subject,String content,String date){Cursor cursor = sqLiteDatabase.rawQuery("select * from memento_tb where subject like ? and content like ? and date like ?",new String[]{"%"+subject+"%","%"+content+"%","%"+date+"%"});return cursor;}
}
(2)MyDatabaseHelper
package com.example.memorandumtest;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;import androidx.annotation.Nullable;public class MyDatabaseHelper extends SQLiteOpenHelper {//建表语句final String CREATE_TABLE_SQL = "create table memento_tb (_id integer primary key autoincrement,subject,content,date)";public MyDatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {super(context, name, factory, version);}@Overridepublic void onCreate(SQLiteDatabase db) {//执行建表语句,创建memento表db.execSQL(CREATE_TABLE_SQL);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {System.out.println("------"+oldVersion+"-------->"+newVersion);}
}
4 实验运行结果图


5 实验总结
首先还是敲定布局文件,一共两个布局文件,第一个是备忘录的整体布局文件,第二个是显示查询结果的布局文件。
接着完成Java代码。在写Java代码的过程中,在选择时间按钮的实现时,先创建一个日历对象,创建日期选择监听器,然后设置当前的年月日,其中月份是从0开始的,所以写代码的时候要加1;关于数据的存储与查找,先创建数据库类工具对象和数据库对象,然后通过调用数据库中添加记录和查询记录的方法来实现;对于查询结果在列表中的显示,创建一个简单游标适配器对象,让列表适用适配器对象。
这个实验还存在一个问题,那就是备忘录的输入只能用英文,而无法采用中文输入,这个问题尚未解决。
