5.2(小组作业十日冲刺第八天)
实现可以点击主页面中的日志并进入,在进入后可以对日志的标题以及内容进行改变,以及实现删除的功能
所花时间:4.5h
代码行量:1354行
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MesActivity"
android:orientation="vertical"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_mes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#e8cb06"
app:title="修改个人信息"
app:navigationIcon="@drawable/back"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/change_pwd">
<LinearLayout
android:background="@drawable/change_style"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="105dp"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:src="@drawable/tx"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp"
/>
<EditText
android:id="@+id/change_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"
android:background="@drawable/change_mes_edit_style"
android:inputType="text"
android:paddingLeft="30dp"
android:paddingRight="40dp"
android:layout_marginBottom="10dp"
android:drawableRight="@drawable/x"
android:paddingTop="15dp"
android:paddingBottom="15dp"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:paddingLeft="30dp"
android:paddingRight="40dp"
>
<RadioButton
android:id="@+id/change_man"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="男"
android:layout_weight="1"
android:textStyle="bold"
android:textColor="#616161"
android:buttonTint="#e8cb06"
/>
<RadioButton
android:id="@+id/change_woman"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="女"
android:layout_weight="1"
android:textStyle="bold"
android:textColor="#616161"
android:buttonTint="#e8cb06"
/>
</RadioGroup>
<EditText
android:id="@+id/change_phone"
android:hint="请输入联系方式"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/change_mes_edit_style"
android:inputType="text"
android:paddingLeft="30dp"
android:paddingRight="40dp"
android:layout_marginBottom="10dp"
android:drawableRight="@drawable/x"
android:paddingTop="15dp"
android:paddingBottom="15dp"
/>
<EditText
android:id="@+id/change_age"
android:hint="请输入年纪"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/change_mes_edit_style"
android:inputType="text"
android:paddingLeft="30dp"
android:paddingRight="40dp"
android:layout_marginBottom="10dp"
android:drawableRight="@drawable/x"
android:paddingTop="15dp"
android:paddingBottom="15dp"
/>
<!-- 移除 android:foreground 属性 -->
<Button
android:id="@+id/change_pwd_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改个人信息"
android:textColor="#FFFFFF"
android:background="@drawable/change_pwd_button_style"
android:foreground="?selectableItemBackground"
android:paddingBottom="15dp"
android:paddingTop="15dp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MesActivity"
android:orientation="vertical"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_mes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="#e8cb06"
app:title="修改个人信息"
app:navigationIcon="@drawable/back"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/change_pwd">
<LinearLayout
android:background="@drawable/change_style"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="105dp"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:src="@drawable/tx"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp"
/>
<EditText
android:id="@+id/change_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"
android:background="@drawable/change_mes_edit_style"
android:inputType="text"
android:paddingLeft="30dp"
android:paddingRight="40dp"
android:layout_marginBottom="10dp"
android:drawableRight="@drawable/x"
android:paddingTop="15dp"
android:paddingBottom="15dp"
/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:paddingLeft="30dp"
android:paddingRight="40dp"
>
<RadioButton
android:id="@+id/change_man"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="男"
android:layout_weight="1"
android:textStyle="bold"
android:textColor="#616161"
android:buttonTint="#e8cb06"
/>
<RadioButton
android:id="@+id/change_woman"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="女"
android:layout_weight="1"
android:textStyle="bold"
android:textColor="#616161"
android:buttonTint="#e8cb06"
/>
</RadioGroup>
<EditText
android:id="@+id/change_phone"
android:hint="请输入联系方式"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/change_mes_edit_style"
android:inputType="text"
android:paddingLeft="30dp"
android:paddingRight="40dp"
android:layout_marginBottom="10dp"
android:drawableRight="@drawable/x"
android:paddingTop="15dp"
android:paddingBottom="15dp"
/>
<EditText
android:id="@+id/change_age"
android:hint="请输入年纪"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/change_mes_edit_style"
android:inputType="text"
android:paddingLeft="30dp"
android:paddingRight="40dp"
android:layout_marginBottom="10dp"
android:drawableRight="@drawable/x"
android:paddingTop="15dp"
android:paddingBottom="15dp"
/>
<!-- 移除 android:foreground 属性 -->
<Button
android:id="@+id/change_pwd_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改个人信息"
android:textColor="#FFFFFF"
android:background="@drawable/change_pwd_button_style"
android:foreground="?selectableItemBackground"
android:paddingBottom="15dp"
android:paddingTop="15dp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<?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="wrap_content"
>
<ImageView
android:src="@drawable/mbg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
</LinearLayout>
<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="wrap_content"
>
<ImageView
android:src="@drawable/mbg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
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="wrap_content"
android:orientation="vertical"
android:background="@drawable/change_mes_manage_style"
android:foreground="?selectableItemBackground"
android:paddingLeft="10dp"
android:layout_marginBottom="10dp"
>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="8dp"
/>
<TextView
android:id="@+id/con"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_marginBottom="5dp"
/>
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:paddingStart="60dp"
/>
</LinearLayout>
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
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="wrap_content"
android:orientation="vertical"
android:background="@drawable/change_mes_manage_style"
android:foreground="?selectableItemBackground"
android:paddingLeft="10dp"
android:layout_marginBottom="10dp"
>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="8dp"
/>
<TextView
android:id="@+id/con"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_marginBottom="5dp"
/>
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:paddingStart="60dp"
/>
</LinearLayout>
package com.example.memosystem.util;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
//alt +回车
public class DBUtil extends SQLiteOpenHelper {
private static final String DATABASE_NAME="db_memo.db";//数据库名字
public static SQLiteDatabase db=null;//用他来操作数据库
private static final int DATABASE_VERSION=3;//每一次对数据库脚本的更新都需要将这个数字进行加1
public DBUtil(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION,null);
}
@Override
public void onCreate(SQLiteDatabase db) {//数据库初始化加载的代码
db.execSQL("PRAGMA foreign_keys = false");
db.execSQL("drop table if exists d_admin");//如果这个表存在则删除
db.execSQL("CREATE TABLE d_admin (s_id varchar(20) primary key," +
" s_pwd varchar(20)," +
" s_name varchar(20)," +
" s_sex varchar(20)," +
" s_phone varchar(20)," +
" s_age varchar(20) )");
db.execSQL("INSERT INTO d_admin VALUES('root','123456','小铭','男','5696331','18')");
//-------------管理员 表
//编号 标题 内容 时间
db.execSQL("drop table if exists d_record");//如果这个表存在则删除
db.execSQL("CREATE TABLE d_record (s_id varchar(50) primary key," +
" s_title varchar(20)," +
" s_con text," +
" s_uid varchar(20)," +//用户ID
" s_time varchar(20) )");
db.execSQL("INSERT INTO d_record VALUES('1','记录一下今天的生活','今天吃了好多零食,看了很多电视剧.','root','2023年9月10日 上午 12:56')");
// db.execSQL("PRAGMA foreign_keys = true");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {//数据库变动则执行
onCreate(sqLiteDatabase);
}
}
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
//alt +回车
public class DBUtil extends SQLiteOpenHelper {
private static final String DATABASE_NAME="db_memo.db";//数据库名字
public static SQLiteDatabase db=null;//用他来操作数据库
private static final int DATABASE_VERSION=3;//每一次对数据库脚本的更新都需要将这个数字进行加1
public DBUtil(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION,null);
}
@Override
public void onCreate(SQLiteDatabase db) {//数据库初始化加载的代码
db.execSQL("PRAGMA foreign_keys = false");
db.execSQL("drop table if exists d_admin");//如果这个表存在则删除
db.execSQL("CREATE TABLE d_admin (s_id varchar(20) primary key," +
" s_pwd varchar(20)," +
" s_name varchar(20)," +
" s_sex varchar(20)," +
" s_phone varchar(20)," +
" s_age varchar(20) )");
db.execSQL("INSERT INTO d_admin VALUES('root','123456','小铭','男','5696331','18')");
//-------------管理员 表
//编号 标题 内容 时间
db.execSQL("drop table if exists d_record");//如果这个表存在则删除
db.execSQL("CREATE TABLE d_record (s_id varchar(50) primary key," +
" s_title varchar(20)," +
" s_con text," +
" s_uid varchar(20)," +//用户ID
" s_time varchar(20) )");
db.execSQL("INSERT INTO d_record VALUES('1','记录一下今天的生活','今天吃了好多零食,看了很多电视剧.','root','2023年9月10日 上午 12:56')");
// db.execSQL("PRAGMA foreign_keys = true");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {//数据库变动则执行
onCreate(sqLiteDatabase);
}
}
package com.example.memosystem.dao;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.memosystem.bean.Record;
import com.example.memosystem.dao.impl.AdminDaoImpl;
import com.example.memosystem.util.DBUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AdminDao implements AdminDaoImpl {
SQLiteDatabase db= DBUtil.db;
@Override
public int registerAdmin(String ...da) {
try {
db.execSQL("INSERT INTO d_admin VALUES(?,?,?,?,?,?)",da);
return 1;
}catch (Exception e){
return -1;
}
}
@Override
public int loginAdmin(String... da) {
int a=0;
Cursor res = db.rawQuery("select * from d_admin where s_id=? and s_pwd=?", da);
if(res.moveToNext()){
a++;
}
return a;
}
/**
* 修改密码 账号 密码
* @param da
* @return
*/
@Override
public int changePassword(String... da) {
ContentValues values=new ContentValues();
values.put("s_pwd", da[1]);
String das[]={da[0]};
try{
return db.update("d_admin",values,"s_id=?",das);
}catch (Exception e){
return -1;
}
}
public int changeMes(String ...da){
ContentValues values=new ContentValues();
values.put("s_name", da[1]);
values.put("s_sex", da[2]);
values.put("s_phone", da[3]);
values.put("s_age", da[4]);
String das[]={da[0]};
try{
return db.update("d_admin",values,"s_id=?",das);
}catch (Exception e){
return -1;
}
}
public Map<String,String> showAccountMes(String account){
Map<String,String> map=new HashMap<>();
String da[]={account};
Cursor result = db.rawQuery("select s_name,s_sex,s_phone,s_age from d_admin where s_id=?", da);
while(result.moveToNext()){
//姓名
//姓名
//手机号
//年纪
String name = result.getString(0);
String sex= result.getString(1);
String phone= result.getString(2);
String age= result.getString(3);
map.put("name",name);
map.put("sex",sex);
map.put("phone",phone);
map.put("age",age);
}
return map;
}
/**
* 删除信息
* @param da
* @return
*/
@Override
public int delRecord(String... da) {
try {
db.execSQL("delete from d_record where s_id=? and s_uid=?",da);
return 1;
}catch (Exception e){
return -1;
}
}
public int saveRecord(String ...da){//id 标题 内容 用户ID 时间
try {
db.execSQL("INSERT INTO d_record VALUES(?,?,?,?,?)",da);
return 1;
}catch (Exception e){
String daa[]={da[1],da[2],da[4],da[0],da[3]};
try {
db.execSQL("update d_record set s_title=? ,s_con=?,s_time=? where s_id=? and s_uid=?",daa);
return 1;
}catch (Exception ee){
e.printStackTrace();
ee.printStackTrace();
return -1;
}
}
}
public List<Record> showRecord(String id){
String da[]={id};
List<Record> re=new ArrayList<>();//存储所有备忘录的
Cursor res = db.rawQuery("select * from d_record where s_uid=?",da);
while(res.moveToNext()){
Record record=new Record(res.getString(0),res.getString(1),res.getString(2),res.getString(3),res.getString(4));
re.add(record);
}
return re;
}
}
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.memosystem.bean.Record;
import com.example.memosystem.dao.impl.AdminDaoImpl;
import com.example.memosystem.util.DBUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AdminDao implements AdminDaoImpl {
SQLiteDatabase db= DBUtil.db;
@Override
public int registerAdmin(String ...da) {
try {
db.execSQL("INSERT INTO d_admin VALUES(?,?,?,?,?,?)",da);
return 1;
}catch (Exception e){
return -1;
}
}
@Override
public int loginAdmin(String... da) {
int a=0;
Cursor res = db.rawQuery("select * from d_admin where s_id=? and s_pwd=?", da);
if(res.moveToNext()){
a++;
}
return a;
}
/**
* 修改密码 账号 密码
* @param da
* @return
*/
@Override
public int changePassword(String... da) {
ContentValues values=new ContentValues();
values.put("s_pwd", da[1]);
String das[]={da[0]};
try{
return db.update("d_admin",values,"s_id=?",das);
}catch (Exception e){
return -1;
}
}
public int changeMes(String ...da){
ContentValues values=new ContentValues();
values.put("s_name", da[1]);
values.put("s_sex", da[2]);
values.put("s_phone", da[3]);
values.put("s_age", da[4]);
String das[]={da[0]};
try{
return db.update("d_admin",values,"s_id=?",das);
}catch (Exception e){
return -1;
}
}
public Map<String,String> showAccountMes(String account){
Map<String,String> map=new HashMap<>();
String da[]={account};
Cursor result = db.rawQuery("select s_name,s_sex,s_phone,s_age from d_admin where s_id=?", da);
while(result.moveToNext()){
//姓名
//姓名
//手机号
//年纪
String name = result.getString(0);
String sex= result.getString(1);
String phone= result.getString(2);
String age= result.getString(3);
map.put("name",name);
map.put("sex",sex);
map.put("phone",phone);
map.put("age",age);
}
return map;
}
/**
* 删除信息
* @param da
* @return
*/
@Override
public int delRecord(String... da) {
try {
db.execSQL("delete from d_record where s_id=? and s_uid=?",da);
return 1;
}catch (Exception e){
return -1;
}
}
public int saveRecord(String ...da){//id 标题 内容 用户ID 时间
try {
db.execSQL("INSERT INTO d_record VALUES(?,?,?,?,?)",da);
return 1;
}catch (Exception e){
String daa[]={da[1],da[2],da[4],da[0],da[3]};
try {
db.execSQL("update d_record set s_title=? ,s_con=?,s_time=? where s_id=? and s_uid=?",daa);
return 1;
}catch (Exception ee){
e.printStackTrace();
ee.printStackTrace();
return -1;
}
}
}
public List<Record> showRecord(String id){
String da[]={id};
List<Record> re=new ArrayList<>();//存储所有备忘录的
Cursor res = db.rawQuery("select * from d_record where s_uid=?",da);
while(res.moveToNext()){
Record record=new Record(res.getString(0),res.getString(1),res.getString(2),res.getString(3),res.getString(4));
re.add(record);
}
return re;
}
}