今天我继续学习了安卓studio的相关内容,之前已经学完了数据库的增删改查,今天进行一个案例练习。
记事本案例
该案例需要创建一个记事本,在记事本里面记录文本和时间信息
主要界面如下
一、首先我进行了主要界面的搭建
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mylayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="每日打卡记事本"
android:id="@+id/title"
android:textSize="30sp"
android:textColor="@color/white"
android:background="#E91E63"
android:gravity="center">
</TextView>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:id="@+id/listview">
</ListView>
<ImageView
android:id="@+id/add"
android:layout_width="69dp"
android:layout_height="114dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="30dp"
android:src="@drawable/add">
</ImageView>
二、接着,因为需要在记事本里面编辑信息,于是我有创建了一个记录信息的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="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/item_content"
android:text="文本信息"
android:textColor="@color/black"
android:textSize="20dp"
android:maxLines="2"
android:ellipsize="end"></TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/item_time"
android:text="时间"
android:textColor="#FF5722"
android:textSize="15dp"></TextView>
</LinearLayout>
处理完界面后,开始考虑如何建立数据库,通过分析可以得到下面结果
如果只建立一个note数据表,不方便调用数量未知的数据,因此需要考虑到建立一个数据模型类,然后利用list列表来实现数据的调用与保存
三、创建一个适配器
由于记事本界面的记录列表都是使用ListView控件展示的,因此需要一个适配器来对控件进行适配
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView==null)
{
convertView =layoutInflater.inflate(R.layout.iteamlayout,null,false);
viewHolder=new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder)convertView.getTag();
}
//将数据库的内容加载到对应的控件上
Note note= (Note) getItem(position);
viewHolder.t_content.setText(note.getContent());
viewHolder.t_time.setText(note.getNote_time());
return convertView;
}
四、创建一个数据库,建立数据表
package com.example.notepad;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyDBhelper extends SQLiteOpenHelper {
private SQLiteDatabase db;
//创建数据库和表
public MyDBhelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
db=this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table note(id integer primary key autoincrement,content text,note_time text)");
}
//对note表的增删改查
//添加数据
public boolean insertData(String content, MainActivity mainActivity)
{
//格式化日期
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年mm月dd日 hh:mm:ss");//格式变成为:2023年02月05日
//获取当前时间
Date date=new Date();
String finalDate=sdf.format(date);
ContentValues contentValues=new ContentValues();
contentValues.put("content",content);
contentValues.put("note_time",finalDate);
long i=db.insert("note",null,contentValues);
if(i>0)
{
return true;
}else{
return false;
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}