记事本App开发阶段二

在阶段二里面, 我编写了书写界面和记事本界面。

写了关于记事本的一套增删改查。

这是关于记事本的一套jdbc

package com.example.textapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.icu.text.DateFormat;
import android.icu.text.SimpleDateFormat;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MydDBhelper extends SQLiteOpenHelper {
    private SQLiteDatabase db;
        //创建数据库和表
    public MydDBhelper(@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 insertDate(String content, Context context){
        //设置日期格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年mm月dd日 hh:mm:ss");
        //获取当前时间,但这个时间是以外文的方式呈现
        Date date = new Date(System.currentTimeMillis());
        //格式化日期
        String time = simpleDateFormat.format(date);
        ContentValues contentValues = new ContentValues();
        contentValues.put("content",content);
        contentValues.put("note_time",time);
        db.insert("note",null,contentValues);
        long i = db.insert("note",null,contentValues);
        if(i>0){
            return true;
        }else{
            return false;
        }
    }
    //删除数据,根据记录的id4进行删除
    public boolean deleteDate(String deleteId){
        int i = db.delete("noteInfo","id = ?",new String[]{deleteId});
        return i>0?true:false;
    }
    //删除数据,能够记录的id进行更新
    public boolean updateDate(String updateId, String updateContent){
        //设置日期格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年mm月dd日 hh:mm:ss");
        //获取当前时间,但这个时间是以外文的方式呈现
        Date date = new Date(System.currentTimeMillis());
        //将需要更新的内容存入contentValues
        String time = simpleDateFormat.format(date);

        ContentValues contentValues = new ContentValues();
        contentValues.put("content",updateContent);
        contentValues.put("time",time);
        int i = db.update("noteInfo",contentValues,"id = ?",new String[]{updateId});
        return i>0?true:false;
    }
    //查询数据,查询表中所有内容,将查询的内容用note的对象属性进行储存,并将对象存入集合中
            public List<Note> query(){
            List<Note> list = new ArrayList<>();
            Cursor cursor = db.query("noteInfo",null,null,null,null,null,null);
            while(cursor.moveToNext()){
                Note note = new Note();
                note.setId(String.valueOf(cursor.getInt(0)));
                note.setContent(cursor.getString(1));
                note.setNote_time(cursor.getString(2));
                list.add(note);
            }
        return list;
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".RecordActivity">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/teal_200"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/backHome"
        android:layout_centerVertical="true"
        android:src="@drawable/back">

    </ImageView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        android:text="记事本"
        android:textSize="30sp"
        android:textColor="@color/white"
        android:layout_toRightOf="@+id/backHome"
        android:layout_centerInParent="true">

    </TextView>
</RelativeLayout>
    <!--添加一个隐藏起来的textview,符合一定条件时候我们让他显示-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/showTime"
        android:textColor="@drawable/back"
        android:layout_gravity="center"
        android:text="显示时间。。。"
        android:visibility="gone"
        android:textSize="30sp">

    </TextView>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/content"
        android:hint="请输入文本信息...">

    </EditText>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal"
            >
            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/delete"
                android:src="@drawable/delete">
            </ImageView>
            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/save_note"
                android:src="@drawable/delete">
            </ImageView>



        </LinearLayout>



</LinearLayout>

 

posted @ 2023-03-01 22:23  YE-  阅读(15)  评论(0编辑  收藏  举报