第一次个人作业-打卡app
package entity; public class Day { public String getKeyword() { return keyword; } public void setKeyword(String keyword) { this.keyword = keyword; } public String keyword; public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String content; public String getCreatedTime() { return createdTime; } public void setCreatedTime(String createdTime) { this.createdTime = createdTime; } public String createdTime; public String getId() { return id; } public void setId(String id) { this.id = id; } public String id; public Day(){}; public Day(String keyword,String content,String createdTime,String id){ this.keyword=keyword; this.content=content; this.createdTime=createdTime; this.id=id; } @Override public String toString() { return "Day{" + "keyword='" + keyword + '\'' + ", content='" + content + '\'' + ", createdTime='" + createdTime + '\'' + ", id='" + id + '\'' + '}'; } }
package com.example.newclock; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import database.UserDBHelper; import entity.User; import org.w3c.dom.Text; import java.util.List; public class SQLiteHelperActivity extends AppCompatActivity implements View.OnClickListener { private EditText et_id; private EditText et_name; private EditText et_ph; private EditText et_class; private TextView tv_show; private UserDBHelper mHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sqlite_helper); et_id=findViewById(R.id.et_id); et_name=findViewById(R.id.et_name); et_ph=findViewById(R.id.et_ph); et_class=findViewById(R.id.et_class); tv_show = findViewById(R.id.tv_show); findViewById(R.id.bt_register).setOnClickListener(this); findViewById(R.id.bt_delete).setOnClickListener(this); findViewById(R.id.bt_update).setOnClickListener(this); findViewById(R.id.bt_all).setOnClickListener(this); } protected void onStart() { super.onStart(); mHelper=UserDBHelper.getInstance(this); mHelper.openWriteLink(); mHelper.openReadLink(); } @Override protected void onStop() { super.onStop(); mHelper.closeLink(); } @Override public void onClick(View view) { String Id=et_id.getText().toString(); String Name=et_name.getText().toString(); String Pn=et_ph.getText().toString(); String Class=et_class.getText().toString(); int a=1; User user=new User(); switch (view.getId()) { case R.id.bt_register: user.setId(Id); user.setName(Name); user.setPn(Pn); user.setCs(Class); if(Pn.length()!=11) { Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show(); return; } if(mHelper.queryOne(Id)>0) { Toast.makeText(this, "该学号已存在", Toast.LENGTH_SHORT).show(); return; } //Log.d("ning",Id); mHelper.insert(user); Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show(); break; case R.id.bt_delete: if(mHelper.deleteById(Id)>0) { Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show(); } break; case R.id.bt_update: user=new User(Id,Name,Pn,Class); if(mHelper.update(user)>0) { Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show(); } break; case R.id.bt_all: List<User> list=mHelper.queryAll(); for(User u:list) { Log.d("ning",u.toString()); } StringBuffer buffer=new StringBuffer(); if(list.size()==0){ tv_show.setText("没有数据"); }else { for (int i=0;i<list.size();i++){ user= list.get(i); buffer.append("id:" +user.getId()+ "用户名:"+user.getName()+ "手机号:"+user.getPn()+ "班级:"+user.getCs()+"\n"); } tv_show.setText(buffer); } break; } } }
package database; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import entity.Day; import entity.User; import java.util.ArrayList; import java.util.List; public class UserDBHelper extends SQLiteOpenHelper { private static final String DB_NAME="user.db"; private static final String TABLE_NAME="user_info"; private static final String TABLE_NAME1="day_info"; private static final int DB_VERSION=1; private static UserDBHelper mHelper=null; private SQLiteDatabase mRDB=null; private SQLiteDatabase mWDB=null; private UserDBHelper(Context context){ super(context,DB_NAME,null,DB_VERSION); } public static UserDBHelper getInstance(Context context){ if(mHelper==null) { mHelper=new UserDBHelper(context); } return mHelper; } public SQLiteDatabase openReadLink(){ if(mRDB==null||!mRDB.isOpen()) { mRDB=mHelper.getReadableDatabase(); } return mRDB; } public SQLiteDatabase openWriteLink(){ if(mWDB==null||!mWDB.isOpen()) { mWDB=mHelper.getWritableDatabase(); } return mWDB; } public void closeLink(){ if(mRDB!=null&&mRDB.isOpen()) { mRDB.close(); mRDB=null; } if(mWDB!=null&&mWDB.isOpen()) { mWDB.close(); mWDB=null; } } @Override public void onCreate(SQLiteDatabase db) { String sql="CREATE TABLE IF NOT EXISTS "+TABLE_NAME+"("+ " Id VARCHAR NOT NULL,"+ " Name VARCHAR NOT NULL,"+ " Pn VARCHAR NOT NULL,"+ " Class VARCHAR NOT NULL)"; db.execSQL(sql); sql="CREATE TABLE IF NOT EXISTS "+TABLE_NAME1+"("+ " keyword VARCHAR NOT NULL,"+ " content VARCHAR NOT NULL,"+ " createdtime VARCHAR NOT NULL,"+ " id VARCHAR NOT NULL)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public void insert(User user) { Log.d("ning",user.Id); ContentValues values=new ContentValues(); values.put("Id",user.Id); values.put("Name",user.Name); values.put("Pn",user.Pn); values.put("Class",user.Cs); mWDB.insert(TABLE_NAME,null,values); } public int insertDay(Day day) { ContentValues values=new ContentValues(); values.put("keyword",day.keyword); values.put("content",day.content); values.put("createdTime",day.createdTime); values.put("id",day.id); mWDB.insert(TABLE_NAME1,null,values); int a=1; return a; } public long deleteById(String Id) { return mWDB.delete(TABLE_NAME,"Id=?",new String[]{Id}); } public long update(User user) { ContentValues values=new ContentValues(); values.put("Id",user.Id); values.put("Name",user.Name); values.put("Pn",user.Pn); values.put("Class",user.Cs); return mWDB.update(TABLE_NAME,values,"Id=?",new String[]{user.Id}); } public List<User> queryAll(){ List<User> list=new ArrayList<>(); Cursor cursor=mRDB.query(TABLE_NAME,null,null,null,null,null,null); while (cursor.moveToNext()){ User user=new User(); user.Id=cursor.getString(0); user.Name=cursor.getString(1); user.Pn=cursor.getString(2); user.Cs=cursor.getString(3); list.add(user); } return list; } public List<Day> queryAllDay(){ List<Day> list=new ArrayList<>(); Cursor cursor=mRDB.query(TABLE_NAME1,null,null,null,null,null,null); while (cursor.moveToNext()){ Day day=new Day(); day.keyword=cursor.getString(0); day.content=cursor.getString(1); day.createdTime=cursor.getString(2); day.id=cursor.getString(3); list.add(day); } return list; } public int queryOne(String Id){ List<User> list1=new ArrayList<>(); Cursor cursor=mRDB.query(TABLE_NAME,null,"Id=?",new String[]{Id},null,null,null); int a=0; while (cursor.moveToNext()){ User user=new User(); user.Id=cursor.getString(0); user.Name=cursor.getString(1); user.Pn=cursor.getString(2); user.Cs=cursor.getString(3); a=1; list1.add(user); } return a; } }
package entity; public class User { public String getId() { return Id; } public void setId(String id) { Id = id; } public String Id; public String getName() { return Name; } public void setName(String name) { Name = name; } public String Name; public String getPn() { return Pn; } public void setPn(String pn) { Pn = pn; } public String Pn; public String getCs() { return Cs; } public void setCs(String cs) { Cs = cs; } public String Cs; public User(){ } public User(String Id,String Name,String Pn,String Cs){ this.Id=Id; this.Name=Name; this.Pn=Pn; this.Cs=Cs; } @Override public String toString() { return "User{" + "Id='" + Id + '\'' + ", Name='" + Name + '\'' + ", Pn='" + Pn + '\'' + ", Cs='" + Cs + '\'' + '}'; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".AddActivity" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关键字" /> <EditText android:id="@+id/et_keyword" android:maxLines="1" android:layout_width="match_parent" android:layout_marginLeft="5dp" android:layout_height="wrap_content"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内容" /> <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:gravity="left" android:layout_height="300dp" android:layout_marginTop="5dp" /> <Button android:id="@+id/bt_add1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加" /> <Button android:id="@+id/bt_all1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="浏览" /> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".LoginActicity" android:orientation="vertical" > <EditText android:id="@+id/et_id1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="学号" android:layout_marginTop="200dp" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="密码(初始密码为学号)" /> <Button android:id="@+id/bt_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录"/> <Button android:id="@+id/bt_register1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注册"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rlv" /> <com.google.android.material.floatingactionbutton.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/ic_input_add" android:layout_gravity="right|bottom" android:layout_margin="20dp" android:id="@+id/bt_add" /> </FrameLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SQLiteHelperActivity" android:orientation="vertical" > <EditText android:id="@+id/et_id" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="160dp" android:hint="学号" /> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="5dp" android:hint="姓名" /> <EditText android:id="@+id/et_ph" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="5dp" android:hint="手机号码" /> <EditText android:id="@+id/et_class" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="5dp" android:hint="班级" /> <Button android:id="@+id/bt_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="add" android:text="注册" /> <Button android:id="@+id/bt_delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除" /> <Button android:id="@+id/bt_update" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="修改" /> <Button android:id="@+id/bt_all" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="浏览" /> <TextView android:id="@+id/tv_show" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/bt_register" android:textSize="10dp" /> </LinearLayout>