目录

要求目的:

效果图:

Android studio sql 数据库查看工具

SQLiteStudio

MainActivity内容:

EditText带icon的布局文件:

定义Myhepler.java ——用户部分——

定义注册登录事件

注册按钮绑定插入用户记录事件:

登录按钮绑定匹配数据库用户密码是否正确并且传值跳转Content页面事件:

ContentActivity:

布局xml文件:

定义Myhelper.java——备忘录数据增删查改——

长按删除数据记录:

短按编辑:

添加备忘录按钮:

onResume()函数实时更新回调函数:

全部代码:


要求目的:

1. 数据库需要存储用户信息、及备忘录信息,

2. 每个用户都有各自的备忘录信息,信息互不干扰

3. 根据按钮 完成数据库 增删查改,实时更新信息

效果图:

Android studio sql 数据库查看工具

SQLiteStudio

下载链接

官网 SQLiteStudiohttps://sqlitestudio.pl/

在本地android studio 上 查询数据库并且复制到桌面用sqlitestudio 工具查看

结果如下:

MainActivity内容:

EditText带icon的布局文件:

xml布局文件

icon 文件

package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.content.SharedPreferences;import android.database.sqlite.SQLiteDatabase;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.text.method.PasswordTransformationMethod;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends AppCompatActivity {private EditText editText;private EditText editText1;Drawable icon;Drawable icon1;private CheckBox checkBox;privateMyHelper myHelper;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main1);editText = findViewById(R.id.loginView);editText1 = findViewById(R.id.passwordView);editText1.setTransformationMethod(PasswordTransformationMethod.getInstance());//密码不可见icon = getResources().getDrawable(R.drawable.profile1);icon1 = getResources().getDrawable(R.drawable.password);icon.setBounds(0, 0, 80, 80);editText.setCompoundDrawables(icon, null, null, null);icon1.setBounds(0,0,80,80);editText1.setCompoundDrawables(icon1, null, null, null);}}

定义Myhepler.java ——用户部分——

建立用户表user_table及备忘录content_table

package com.example.myapplication;import android.annotation.SuppressLint;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.widget.Toast;import androidx.annotation.Nullable;import java.util.ArrayList;import java.util.List;public class MyHelper extends SQLiteOpenHelper {private static final String dbname = "memo.db";private static finalString UserTableName = "user_table";private staticfinalString ContentTableName = "content_table";private List memolist = new ArrayList();public MyHelper(@Nullable Context context) {super(context,dbname, null, 2);}@Overridepublic void onCreate(SQLiteDatabase sqLiteDatabase) {String sql_create1 = "CREATE TABLE IF NOT EXISTS "+ UserTableName +"("+"_id integer primary key autoincrement,username varchar not null,password varchar not null"+");";sqLiteDatabase.execSQL(sql_create1);String sql_create2 = "CREATE TABLE IF NOT EXISTS "+ ContentTableName +"("+"_id integer primary key autoincrement,username varchar not null,memotitle varchar not null"+");";sqLiteDatabase.execSQL(sql_create2);}}

插入用户信息:

//注册事件 插入user记录protectedvoid insert_user(user user,SQLiteDatabase sqLiteDatabase){sqLiteDatabase.execSQL("insert into user_table "+ "(username,password)"+ "values(?,?)",new String[]{user.getUsername(), user.getPassword()});}

匹配用户及密码信息:

//查询数据库 先按username 筛选记录 再与password字段匹配 ,如果不存在记录也是返回false@SuppressLint("Range")protected boolean queryuser(user user, SQLiteDatabase sqLiteDatabase){Cursor cursor = sqLiteDatabase.rawQuery("select * from user_table where username = ? and password = ?", new String[]{user.getUsername(), user.getPassword()});if(cursor.moveToNext()) {return true;}return false;}

定义注册登录事件

注册按钮绑定插入用户记录事件:

myHelper = new MyHelper(MainActivity.this);db = myHelper.getWritableDatabase();register = findViewById(R.id.registerbutton);//注册事件register.setOnClickListener(view -> {user user = new user(editText.getText().toString(),editText1.getText().toString());myHelper.insert_user(user,db);Toast.makeText(MainActivity.this,"注册成功",Toast.LENGTH_LONG).show();});

登录按钮绑定匹配数据库用户密码是否正确并且传值跳转Content页面事件:

login.setOnClickListener(view -> {user user = new user(editText.getText().toString(),editText1.getText().toString());if (myHelper.queryuser(user,db)){Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_LONG).show();Intent intent = new Intent(MainActivity.this,ContentActivity.class);intent.putExtra("username",editText.getText().toString());startActivity(intent);}else {Toast.makeText(MainActivity.this,"用户名或密码错误",Toast.LENGTH_LONG).show();}});

ContentActivity:

布局xml文件:

定义Myhelper.java——备忘录数据增删查改——

//查询备忘录并且返回listprotectedList queryMemo(String username,SQLiteDatabase db){memolist.clear();Cursor cursor = db.rawQuery("select * from content_table where username = ? ", new String[]{username});while (cursor.moveToNext()){memolist.add(cursor.getString(2));}return memolist;}//插入备忘录protected void insert_title(String username ,String title ,SQLiteDatabase db){db.execSQL("insert into content_table "+ "(username,memotitle)"+ "values(?,?)",new String[]{username, title});}//删除备忘录public void delete_content(String username ,String title ,SQLiteDatabase db){String sql = "delete from "+ ContentTableName +" where username like '"+username+"' and memotitle like '"+title+"';";db.execSQL(sql);}//更新备忘录public void update_content(String username,String old_tit,String new_tit,SQLiteDatabase db){String sql = "update "+ ContentTableName +" set memotitle = '"+new_tit+"'where username like '"+username+"' and memotitle like '"+old_tit+"';";db.execSQL(sql);}

长按删除数据记录:

//列表点击事件//长按listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView adapterView, View view, int i, long l) {final String content = ((TextView) view).getText().toString();AlertDialog.Builder editDialog = new AlertDialog.Builder(ContentActivity.this);editDialog.setTitle(content);editDialog.setMessage("是否删除该备忘录");editDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {myHelper.delete_content(username, content, db);Toast.makeText(ContentActivity.this, "数据已删除", Toast.LENGTH_LONG).show();onResume();}});editDialog.show();return true;}});

短按编辑:

//短按事件listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView adapterView, View view, int i, long l) {final String content = ((TextView) view).getText().toString();final EditText editText = new EditText(ContentActivity.this);editText.setText(memolist.get(i));AlertDialog.Builder editDialog = new AlertDialog.Builder(ContentActivity.this);editDialog.setView(editText);editDialog.setTitle("请重新输入内容:");editDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {String newtitle = editText.getText().toString();if (newtitle.length() > 0) {myHelper.update_content(username, content, newtitle, db);Toast.makeText(ContentActivity.this, "数据已存入", Toast.LENGTH_LONG).show();onResume();} else {Toast.makeText(ContentActivity.this, "数据为空", Toast.LENGTH_LONG).show();}}});editDialog.show();}});

添加备忘录按钮:

 //添加备忘录按键button = findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {final EditText editText = new EditText(ContentActivity.this);AlertDialog.Builder editalertlog = new AlertDialog.Builder(ContentActivity.this);editalertlog.setTitle("请输入备忘录内容:");editalertlog.setView(editText);editalertlog.setPositiveButton("确认", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {String title = editText.getText().toString();if(title.length()>0){myHelper.insert_title(username,title,db);Toast.makeText(ContentActivity.this,"数据已添加",Toast.LENGTH_LONG).show();onResume();}else {Toast.makeText(ContentActivity.this,"输入数据为空",Toast.LENGTH_LONG).show();}}});editalertlog.show();}});

onResume()函数实时更新回调函数:

注意:这个回调函数涉及到界面备忘录数据的显示,另外listview 需要适配器,才能绑定数据

@Overrideprotected void onResume() {super.onResume();if (db ==null || !db.isOpen()) {db = myHelper.getWritableDatabase();}memolist = myHelper.queryMemo(username,db);listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,memolist));}

全部代码:

MainActivity.java:

ackage com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.content.SharedPreferences;import android.database.sqlite.SQLiteDatabase;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.text.method.PasswordTransformationMethod;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends AppCompatActivity {private EditText editText;private EditText editText1;Drawable icon;Drawable icon1;private CheckBox checkBox;privateMyHelper myHelper;private SharedPreferences myShare;private SQLiteDatabase db ;private Button login;private Button register;@Overrideprotected void onStart() {super.onStart();myShare = getSharedPreferences("save_login",MODE_PRIVATE);Boolean checked = myShare.getBoolean("checkBox",false);String name = myShare.getString("username",null);String password = myShare.getString("password",null);if(checked){editText.setText("");editText1.setText("");}else{editText.setText(name);editText1.setText(password);}}@Overrideprotected void onStop() {super.onStop();//myHelper.closeLink();myShare = getSharedPreferences("save_login",MODE_PRIVATE);SharedPreferences.Editor myEdit = myShare.edit();if(checkBox.isChecked()){myEdit.putString("username",editText.getText().toString());myEdit.putString("password",editText1.getText().toString());myEdit.putBoolean("checksave",true);myEdit.commit();}else {myEdit.putBoolean("checksave",false);myEdit.clear().commit();}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main1);editText = findViewById(R.id.loginView);editText1 = findViewById(R.id.passwordView);editText1.setTransformationMethod(PasswordTransformationMethod.getInstance());//密码不可见icon = getResources().getDrawable(R.drawable.profile1);icon1 = getResources().getDrawable(R.drawable.password);icon.setBounds(0, 0, 80, 80);editText.setCompoundDrawables(icon, null, null, null);icon1.setBounds(0,0,80,80);editText1.setCompoundDrawables(icon1, null, null, null);checkBox = findViewById(R.id.checkBox);myHelper = new MyHelper(MainActivity.this);db = myHelper.getWritableDatabase();login = findViewById(R.id.loginbutton);register = findViewById(R.id.registerbutton);login.setOnClickListener(view -> {user user = new user(editText.getText().toString(),editText1.getText().toString());if (myHelper.queryuser(user,db)){Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_LONG).show();Intent intent = new Intent(MainActivity.this,ContentActivity.class);intent.putExtra("username",editText.getText().toString());startActivity(intent);}else {Toast.makeText(MainActivity.this,"用户名或密码错误",Toast.LENGTH_LONG).show();}});register.setOnClickListener(view -> {user user = new user(editText.getText().toString(),editText1.getText().toString());myHelper.insert_user(user,db);Toast.makeText(MainActivity.this,"注册成功",Toast.LENGTH_LONG).show();});}}

ContentActivity.java

package com.example.myapplication;import androidx.appcompat.app.AlertDialog;import androidx.appcompat.app.AppCompatActivity;import android.content.DialogInterface;import android.content.Intent;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import java.util.ArrayList;import java.util.List;public class ContentActivity extends AppCompatActivity {private ListView listView;private String username;private String title;private MyHelper myHelper;private SQLiteDatabase db;private List memolist = new ArrayList();private Button button;private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_content);listView = findViewById(R.id.listview);//接受MainActivity的传值Intent intent = getIntent();username = intent.getStringExtra("username");myHelper =new MyHelper(this);textView = findViewById(R.id.textView);textView.setText(username+"的备忘录");//列表点击事件//长按listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView adapterView, View view, int i, long l) {final String content = ((TextView) view).getText().toString();AlertDialog.Builder editDialog = new AlertDialog.Builder(ContentActivity.this);editDialog.setTitle(content);editDialog.setMessage("是否删除该备忘录");editDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {myHelper.delete_content(username, content, db);Toast.makeText(ContentActivity.this, "数据已删除", Toast.LENGTH_LONG).show();onResume();}});editDialog.show();return true;}});//短按事件listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView adapterView, View view, int i, long l) {final String content = ((TextView) view).getText().toString();final EditText editText = new EditText(ContentActivity.this);editText.setText(memolist.get(i));AlertDialog.Builder editDialog = new AlertDialog.Builder(ContentActivity.this);editDialog.setView(editText);editDialog.setTitle("请重新输入内容:");editDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {String newtitle = editText.getText().toString();if (newtitle.length() > 0) {myHelper.update_content(username, content, newtitle, db);Toast.makeText(ContentActivity.this, "数据已存入", Toast.LENGTH_LONG).show();onResume();} else {Toast.makeText(ContentActivity.this, "数据为空", Toast.LENGTH_LONG).show();}}});editDialog.show();}});//添加备忘录按键button = findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {final EditText editText = new EditText(ContentActivity.this);AlertDialog.Builder editalertlog = new AlertDialog.Builder(ContentActivity.this);editalertlog.setTitle("请输入备忘录内容:");editalertlog.setView(editText);editalertlog.setPositiveButton("确认", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialogInterface, int i) {String title = editText.getText().toString();if(title.length()>0){myHelper.insert_title(username,title,db);Toast.makeText(ContentActivity.this,"数据已添加",Toast.LENGTH_LONG).show();onResume();}else {Toast.makeText(ContentActivity.this,"输入数据为空",Toast.LENGTH_LONG).show();}}});editalertlog.show();}});}/** * */@Overrideprotected void onResume() {super.onResume();if (db ==null || !db.isOpen()) {db = myHelper.getWritableDatabase();}memolist = myHelper.queryMemo(username,db);listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,memolist));}/** * */@Overrideprotected void onStart() {super.onStart();}/** * */@Overrideprotected void onStop() {super.onStop();}}

Myhelper.java

package com.example.myapplication;import android.annotation.SuppressLint;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.widget.Toast;import androidx.annotation.Nullable;import java.util.ArrayList;import java.util.List;public class MyHelper extends SQLiteOpenHelper {private static final String dbname = "memo.db";private static finalString UserTableName = "user_table";private staticfinalString ContentTableName = "content_table";private List memolist = new ArrayList();public MyHelper(@Nullable Context context) {super(context,dbname, null, 2);}@Overridepublic void onCreate(SQLiteDatabase sqLiteDatabase) {String sql_create1 = "CREATE TABLE IF NOT EXISTS "+ UserTableName +"("+"_id integer primary key autoincrement,username varchar not null,password varchar not null"+");";sqLiteDatabase.execSQL(sql_create1);String sql_create2 = "CREATE TABLE IF NOT EXISTS "+ ContentTableName +"("+"_id integer primary key autoincrement,username varchar not null,memotitle varchar not null"+");";sqLiteDatabase.execSQL(sql_create2);}@Overridepublic void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {String drop = "DROP TABLE IF EXISTS "+ UserTableName +";";sqLiteDatabase.execSQL(drop);drop = "DROP TABLE IF EXISTS "+ContentTableName +";";sqLiteDatabase.execSQL(drop);onCreate(sqLiteDatabase);}//查询数据库 先按username 筛选记录 再与password字段匹配 ,如果不存在记录也是返回false@SuppressLint("Range")protected boolean queryuser(user user, SQLiteDatabase sqLiteDatabase){Cursor cursor = sqLiteDatabase.rawQuery("select * from user_table where username = ? and password = ?", new String[]{user.getUsername(), user.getPassword()});if(cursor.moveToNext()) {return true;}return false;}//注册事件 插入user记录protectedvoid insert_user(user user,SQLiteDatabase sqLiteDatabase){sqLiteDatabase.execSQL("insert into user_table "+ "(username,password)"+ "values(?,?)",new String[]{user.getUsername(), user.getPassword()});}//查询备忘录并且返回listprotectedList queryMemo(String username,SQLiteDatabase db){memolist.clear();Cursor cursor = db.rawQuery("select * from content_table where username = ? ", new String[]{username});while (cursor.moveToNext()){memolist.add(cursor.getString(2));}return memolist;}//插入备忘录protected void insert_title(String username ,String title ,SQLiteDatabase db){db.execSQL("insert into content_table "+ "(username,memotitle)"+ "values(?,?)",new String[]{username, title});}//删除备忘录public void delete_content(String username ,String title ,SQLiteDatabase db){String sql = "delete from "+ ContentTableName +" where username like '"+username+"' and memotitle like '"+title+"';";db.execSQL(sql);}//更新备忘录public void update_content(String username,String old_tit,String new_tit,SQLiteDatabase db){String sql = "update "+ ContentTableName +" set memotitle = '"+new_tit+"'where username like '"+username+"' and memotitle like '"+old_tit+"';";db.execSQL(sql);}}

user.java:

package com.example.myapplication;public class user {privateString username;privateString password;public user(String username, String password){this.username = username;this.password = password;}public String getUsername() {return username;}public String getPassword() {return password;}}

activity_content.xml:

activity_main.xml