소개
Sqlite는 계정 및 노트 정보를 저장하는 데이터베이스로 사용되며 계정 등록 및 로그인 기능을 구현하고 노트 추가, 삭제, 수정, 검색 기능도 구현합니다. 노트의 목록 표시는 listView를 사용합니다. 따라서 listView의 기본 구분선을 사용하세요);
실행 결과
코드 설명
내 코드에는 두 가지 종속성을 사용하는데, 하나는 도구 클래스이고 다른 하나는 제목 표시줄입니다. 도구 클래스는 내부의 SPUtils를 사용하여 현재 로그인된 계정 정보를 저장합니다. 계정 비밀번호를 다시 입력할 필요가 없습니다. 필요에 따라 소개됩니다.
//标题栏
implementation 'com.github.FlyJingFish:TitleBar:1.2.5'
//工具类
implementation 'com.blankj:utilcodex:1.31.1'
첫 번째는 데이터베이스의 구현 부분으로, 새로운 DBOpenHelper 클래스를 생성하고 SQLiteOpenHelper를 상속합니다.
public class DBOpenHelper extends SQLiteOpenHelper {
private DBOpenHelper dbOpenHelper;
private SQLiteDatabase db;
private static final String DBNAME="notes.db";
private static final int VERSION=1;
public DBOpenHelper(Context context) {
super(context, DBNAME, null, VERSION);
dbOpenHelper=this;
db=dbOpenHelper.getWritableDatabase();
}
//创建数据库
@Override
public void onCreate(SQLiteDatabase db) {
//创建数据表
db.execSQL("create table if not exists user(id INTEGER primary key autoincrement,username varchar(25),password varchar(20))");
db.execSQL("create table if not exists notes(id INTEGER primary key autoincrement,title varchar(20),content varchar(255),time varchar(20)," +
"username varchar(20))");
}
//升级数据库
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
//插入用户数据
public boolean insertUserData(String username,String password){
ContentValues contentValues=new ContentValues();
contentValues.put("username",username);
contentValues.put("password",password);
return db.insert("user",null,contentValues)>0;
}
public boolean insertNotes(String title,String content,String time,String username){
ContentValues contentValues=new ContentValues();
contentValues.put("title",title);
contentValues.put("content",content);
contentValues.put("time",time);
contentValues.put("username",username);
return db.insert("notes",null,contentValues)>0;
}
public boolean updateNotes(String id,String title,String content,String time){
ContentValues contentValues=new ContentValues();
contentValues.put("title",title);
contentValues.put("content",content);
contentValues.put("time",time);
String sql="id=?";
String[] strings=new String[]{
id};
return db.update("notes",contentValues,sql,strings)>0;
}
public boolean deleteNotes(String id){
String sql="id=?";
String[] contentValuesArray=new String[]{
id};
return db.delete("notes",sql,contentValuesArray)>0;
}
//获取笔记
public List<Notes> getNotes(String query){
List<Notes> list=new ArrayList<>();
Cursor cursor;
if (query==null){
cursor=db.rawQuery("select * from notes where username =?",new String[]{
SPUtils.getInstance().getString("username")});
}
else {
cursor=db.rawQuery("select * from notes where username =? and title like ?",new String[]{
SPUtils.getInstance().getString("username"),"%"+query+"%"});
}
if (cursor!=null){
while (cursor.moveToNext()){
@SuppressLint("Range") int id=cursor.getInt(cursor.getColumnIndex("id"));
@SuppressLint("Range") String title=cursor.getString(cursor.getColumnIndex("title"));
@SuppressLint("Range") String content=cursor.getString(cursor.getColumnIndex("content"));
@SuppressLint("Range") String time=cursor.getString(cursor.getColumnIndex("time"));
@SuppressLint("Range") String user=cursor.getString(cursor.getColumnIndex("username"));
Notes note=new Notes(id,title,content,time,user);
list.add(note);
}
cursor.close();
}
return list;
}
}
그런 다음 제목 표시줄과 데이터베이스의 통합 관리를 용이하게 하는 기본 클래스 Activity가 있습니다.
BaseActivity 생성 및 AppCompatActivity 상속
public class BaseActivity extends AppCompatActivity {
protected TitleBar titleBar;
protected DBOpenHelper dbOpenHelper;
protected SQLiteDatabase db;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initTitle();
dbOpenHelper=new DBOpenHelper(this);
db=dbOpenHelper.getWritableDatabase();
}
public void initTitle(){
titleBar = new TitleBar(this);
titleBar.setShadow(1, Color.parseColor("#40454545"), TitleBar.ShadowType.GRADIENT);
titleBar.setTitleGravity(TitleBar.TitleGravity.CENTER);
titleBar.setOnBackViewClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
titleBar.setAboveContent(true);
titleBar.attachToWindow();
}
@Override
protected void onDestroy() {
super.onDestroy();
db.close();
dbOpenHelper.close();
}
}
그런 다음 메모 엔터티 클래스가 있습니다.
public class Notes implements Serializable {
private int id;
private String title;
private String content;
private String time;
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Notes() {
}
public Notes(int id, String title, String content, String time,String user) {
this.id = id;
this.title = title;
this.content = content;
this.time = time;
}
@Override
public String toString() {
return "Notes{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
", time='" + time + '\'' +
", username='" + username + '\'' +
'}';
}
}
로그인 등록 페이지
로그인 페이지
public class Login extends BaseActivity {
private EditText username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
titleBar.setTitle("登录");
titleBar.setDisplayLeftView(false);
username=findViewById(R.id.et_user_name);
password=findViewById(R.id.et_psw);
String name=SPUtils.getInstance().getString("username");
String pw=SPUtils.getInstance().getString("password");
if (!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(pw)){
username.setText(name);
password.setText(pw);
}
//点击登录
findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
login();
}
});
//去注册
findViewById(R.id.btn_register).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(Login.this, Register.class);
intent.putExtra("flag","login");
startActivity(intent);
}
});
}
public void login(){
String name =username.getText().toString();
String pw =password.getText().toString();
if (name.equals("")||pw.equals("")){
Toast.makeText(Login.this,"请输入账号密码!",Toast.LENGTH_SHORT).show();
}
else {
//查询用户
@SuppressLint("Recycle") Cursor cursor=db.rawQuery("select * from user where username=?",new String[]{
name});
cursor.moveToFirst();
if (cursor.getCount()==0){
Toast.makeText(Login.this,"不存在该用户!",Toast.LENGTH_SHORT).show();
}
else{
@SuppressLint("Range") String password=cursor.getString(cursor.getColumnIndex("password"));
if (pw.equals(password)){
Toast.makeText(Login.this,"登录成功!",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
//保存用户名到SharedPreferences
SPUtils.getInstance().put("username",name);
SPUtils.getInstance().put("password",pw);
}else {
Toast.makeText(Login.this,"密码错误!",Toast.LENGTH_SHORT).show();
}
}
}
}
}
로그인 페이지 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".ui.Login">
<EditText
android:id="@+id/et_user_name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/edittext_style"
android:gravity="center_vertical"
android:hint="请输入用户名"
android:paddingLeft="15dp"
android:layout_marginHorizontal="20dp"
android:singleLine="true"
android:textColor="@color/black"
android:textSize="14sp" />
<!--输入框-->
<EditText
android:id="@+id/et_psw"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/edittext_style"
android:gravity="center_vertical"
android:hint="请输入密码"
android:paddingLeft="15dp"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="10dp"
android:inputType="textPassword"
android:singleLine="true"
android:textColor="@color/black"
android:textSize="14sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_login"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:background="@drawable/button_style"
android:text="登 录"
android:textColor="@android:color/white"
android:textSize="18sp" />
<Button
android:id="@+id/btn_register"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="15dp"
android:layout_marginRight="35dp"
android:background="@drawable/button_style"
android:text="注册"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
등록 페이지
public class Register extends BaseActivity {
private EditText username,password,pw_again;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
titleBar.setTitle("欢迎注册");
username=findViewById(R.id.et_user_name);
password=findViewById(R.id.et_psw);
pw_again=findViewById(R.id.et_psw_again);
//注册
findViewById(R.id.btn_register).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
register();
}
});
}
public void register(){
String name =username.getText().toString();
String pw =password.getText().toString();
String pwa =pw_again.getText().toString();
if (name.equals("")||pw.equals("")||pwa.equals("")){
Toast.makeText(Register.this,"请输入完整!",Toast.LENGTH_SHORT).show();
}
else {
//查询用户
Cursor cursor = db.rawQuery("select * from user where username=?", new String[]{
name});
cursor.moveToFirst();
if (cursor.getCount() == 0) {
if (!pw.equals(pwa)) {
Toast.makeText(Register.this, "两次密码不相同!", Toast.LENGTH_SHORT).show();
} else {
if (dbOpenHelper.insertUserData(name, pw)){
Toast.makeText(Register.this, "注册成功!", Toast.LENGTH_SHORT).show();
finish();
}
}
} else {
Toast.makeText(Register.this, "该用户已存在!", Toast.LENGTH_SHORT).show();
}
}
}
}
첫 장
첫 장
public class MainActivity extends BaseActivity {
private List<Notes> noteList=new ArrayList<>();
private ListView listView;
private NoteAdapter noteAdapter;
private SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
titleBar.setTitle("我的记事本");
titleBar.getRightTextView().setText("新增");
titleBar.setOnRightViewClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, AddNote.class));
}
});
listView=findViewById(R.id.listView);
searchView=findViewById(R.id.searchView);
//初始化适配器
noteAdapter=new NoteAdapter(this,R.layout.note_item, noteList);
//设置适配器
listView.setAdapter(noteAdapter);
//搜索笔记
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if (!TextUtils.isEmpty(query)){
noteList.clear();
noteList.addAll(dbOpenHelper.getNotes(query));
noteAdapter.notifyDataSetChanged();
searchView.clearFocus();
}
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)){
noteList.clear();
noteList.addAll(dbOpenHelper.getNotes(null));
noteAdapter.notifyDataSetChanged();
searchView.clearFocus();
}
return false;
}
});
}
@Override
protected void onResume() {
super.onResume();
noteList.clear();
noteList.addAll(dbOpenHelper.getNotes(null));
noteAdapter.notifyDataSetChanged();
}
}
홈 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<androidx.appcompat.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:iconifiedByDefault="false"
android:background="@drawable/edittext_style"
app:queryHint="输入内容进行搜索"
android:layout_marginHorizontal="20dp"/>
<ListView
android:id="@+id/listView"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
마지막은 새 노트 페이지를 추가하는 것입니다.
새로운 추가와 수정이 유사하므로 모두 한 페이지에 배치하고 플래그를 기준으로 새로운 추가 또는 수정이 이루어졌는지 판단할 수 있습니다.
public class AddNote extends BaseActivity {
private EditText title,content;
private TextView number,time;
private String flag;
private Notes note;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_note);
title=findViewById(R.id.title);
content=findViewById(R.id.content);
number=findViewById(R.id.number);
time=findViewById(R.id.time);
flag = getIntent().getStringExtra("flag");
titleBar.getRightTextView().setText("保存");
if (flag!=null){
note = (Notes) getIntent().getSerializableExtra("entity");
title.setText(note.getTitle());
content.setText(note.getContent());
time.setText("上次修改时间:"+ note.getTime());
number.setText(note.getContent().length()+"字");
titleBar.setTitle("修改记事本");
}
//设置时间
else {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss", Locale.CHINA);
time.setText(simpleDateFormat.format(date));
titleBar.setTitle("新增记事本");
}
//更新字数
content.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
number.setText(content.getText().toString().length()+"字");
}
@Override
public void afterTextChanged(Editable editable) {
}
});
titleBar.setOnRightViewClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss", Locale.CHINA);
if (!"".equals(title.getText().toString())&&!"".equals(content.getText().toString())){
if (flag==null){
if (dbOpenHelper.insertNotes(title.getText().toString(),content.getText().toString(),simpleDateFormat.format(date), SPUtils.getInstance().getString("username"))){
Toast.makeText(AddNote.this, "保存成功!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(AddNote.this, "未知错误", Toast.LENGTH_SHORT).show();
}
}
else {
if (dbOpenHelper.updateNotes(String.valueOf(note.getId()),title.getText().toString(),content.getText().toString(),simpleDateFormat.format(date))){
Toast.makeText(AddNote.this, "修改成功!", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(AddNote.this, "未知错误", Toast.LENGTH_SHORT).show();
}
}
finish();
}
else {
Toast.makeText(AddNote.this, "请输入内容!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
XML 파일
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".ui.AddNote">
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="16sp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.065"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0字"
android:textSize="16sp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.077"
app:layout_constraintStart_toEndOf="@+id/time"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="标题"
android:inputType="text"
android:lines="1"
android:maxLength="10"
android:paddingLeft="10dp"
android:textColor="@color/black"
android:textSize="30sp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/number"
android:autofillHints="" />
<EditText
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:hint="内容"
android:background="@null"
android:gravity="start"
android:textSize="25sp"
android:paddingStart="10dp"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title"
android:autofillHints="" />
</androidx.constraintlayout.widget.ConstraintLayout>
마지막으로 listView용 어댑터가 있습니다.
//listview适配器
public class NoteAdapter extends ArrayAdapter<Notes> {
private Context context;
private DBOpenHelper dbOpenHelper;
//构造方法
public NoteAdapter(@NonNull Context context, int resource, List<Notes> diaryList) {
super(context, resource,diaryList);
this.context=context;
dbOpenHelper=new DBOpenHelper(context);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Notes note=getItem(position);
@SuppressLint("ViewHolder") View view= LayoutInflater.from(getContext()).inflate(R.layout.note_item,parent,false);
TextView title = view.findViewById(R.id.title);
TextView create_time = view.findViewById(R.id.create_time);
TextView content=view.findViewById(R.id.content);
ImageView del = view.findViewById(R.id.del);
LinearLayout constraintLayout=view.findViewById(R.id.con);
//设置item内容
title.setText(note.getTitle());
content.setText(note.getContent());
create_time.setText(note.getTime());
//删除item
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (dbOpenHelper.deleteNotes(String.valueOf(note.getId()))){
Toast.makeText(context,"已删除",Toast.LENGTH_SHORT).show();
remove(note);
notifyDataSetChanged();
}
else {
Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();
}
}
});
//单击item
constraintLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(context, AddNote.class);
//设置flag
intent.putExtra("flag","update");
//将笔记内容传递过去
intent.putExtra("entity",note);
context.startActivity(intent);
}
});
return view;
}
}
기본적으로 코드는 이게 전부이고, 궁금한 사항은 댓글로 남겨주세요.
소스 코드
github:https://github.com/panzhusheng/Notes
gitee:https:/ /gitee.com/pan-zs/notes