当前位置:   article > 正文

Android Studio入门实战--实现数据库增删查改 备忘录实例_androidstudio询百度云数据库数据

androidstudio询百度云数据库数据

目录

要求目的:

效果图:

Android studio sql 数据库查看工具

SQLiteStudio

MainActivity内容:

EditText带icon的布局文件:

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

定义注册登录事件

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

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

 ContentActivity:

布局xml文件:

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

长按删除数据记录:

短按编辑:

添加备忘录按钮:

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

全部代码:


要求目的:

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

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

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

效果图:

                

Android studio sql 数据库查看工具

SQLiteStudio

下载链接

 官网           SQLiteStudioicon-default.png?t=N7T8https://sqlitestudio.pl/

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

结果如下:

MainActivity内容:

EditText带icon的布局文件:

                xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="@drawable/img"
  8. tools:context=".MainActivity">
  9. <ImageView
  10. android:id="@+id/imageView"
  11. android:layout_width="122dp"
  12. android:layout_height="109dp"
  13. android:layout_marginStart="152dp"
  14. android:layout_marginBottom="44dp"
  15. android:src="@drawable/userimg"
  16. app:layout_constraintBottom_toTopOf="@+id/loginView"
  17. app:layout_constraintStart_toStartOf="parent" />
  18. <EditText
  19. android:id="@+id/loginView"
  20. android:layout_width="235dp"
  21. android:layout_height="51dp"
  22. android:layout_marginStart="108dp"
  23. android:layout_marginTop="232dp"
  24. android:background="@android:drawable/editbox_background"
  25. android:hint=" Enter your name"
  26. app:layout_constraintStart_toStartOf="parent"
  27. app:layout_constraintTop_toTopOf="parent" />
  28. <EditText
  29. android:id="@+id/passwordView"
  30. android:layout_width="240dp"
  31. android:layout_height="50dp"
  32. android:layout_marginStart="104dp"
  33. android:layout_marginTop="36dp"
  34. android:background="@android:drawable/editbox_background"
  35. android:hint=" Enter your password"
  36. app:layout_constraintStart_toStartOf="parent"
  37. app:layout_constraintTop_toBottomOf="@+id/loginView" />
  38. <Button
  39. android:id="@+id/loginbutton"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_marginStart="84dp"
  43. android:layout_marginTop="144dp"
  44. android:text="登录"
  45. app:layout_constraintStart_toStartOf="parent"
  46. app:layout_constraintTop_toBottomOf="@+id/passwordView" />
  47. <Button
  48. android:id="@+id/registerbutton"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_marginTop="144dp"
  52. android:layout_marginEnd="40dp"
  53. android:text="注册"
  54. app:layout_constraintEnd_toEndOf="parent"
  55. app:layout_constraintTop_toBottomOf="@+id/passwordView" />
  56. <CheckBox
  57. android:id="@+id/checkBox"
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:layout_marginTop="68dp"
  61. android:layout_marginEnd="36dp"
  62. android:text="记住密码"
  63. android:textAppearance="@style/TextAppearance.AppCompat.Medium"
  64. app:layout_constraintEnd_toEndOf="parent"
  65. app:layout_constraintTop_toBottomOf="@+id/passwordView" />
  66. </androidx.constraintlayout.widget.ConstraintLayout>

        icon 文件

  1. package com.example.myapplication;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.graphics.drawable.Drawable;
  7. import android.os.Bundle;
  8. import android.text.method.PasswordTransformationMethod;
  9. import android.widget.Button;
  10. import android.widget.CheckBox;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13. public class MainActivity extends AppCompatActivity {
  14. private EditText editText;
  15. private EditText editText1;
  16. Drawable icon;
  17. Drawable icon1;
  18. private CheckBox checkBox;
  19. private MyHelper myHelper;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main1);
  24. editText = findViewById(R.id.loginView);
  25. editText1 = findViewById(R.id.passwordView);
  26. editText1.setTransformationMethod(PasswordTransformationMethod.getInstance());//密码不可见
  27. icon = getResources().getDrawable(R.drawable.profile1);
  28. icon1 = getResources().getDrawable(R.drawable.password);
  29. icon.setBounds(0, 0, 80, 80);
  30. editText.setCompoundDrawables(icon, null, null, null);
  31. icon1.setBounds(0,0,80,80);
  32. editText1.setCompoundDrawables(icon1, null, null, null);
  33. }
  34. }

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

       建立用户表user_table及备忘录content_table

  1. package com.example.myapplication;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7. import android.widget.Toast;
  8. import androidx.annotation.Nullable;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. public class MyHelper extends SQLiteOpenHelper {
  12. private static final String dbname = "memo.db";
  13. private static final String UserTableName = "user_table";
  14. private static final String ContentTableName = "content_table";
  15. private List<String> memolist = new ArrayList<>();
  16. public MyHelper(@Nullable Context context) {
  17. super(context,dbname, null, 2);
  18. }
  19. @Override
  20. public void onCreate(SQLiteDatabase sqLiteDatabase) {
  21. String sql_create1 = "CREATE TABLE IF NOT EXISTS "+ UserTableName +"("+"_id integer primary key autoincrement,username varchar not null,password varchar not null"+");";
  22. sqLiteDatabase.execSQL(sql_create1);
  23. String sql_create2 = "CREATE TABLE IF NOT EXISTS "+ ContentTableName +"("+"_id integer primary key autoincrement,username varchar not null,memotitle varchar not null"+");";
  24. sqLiteDatabase.execSQL(sql_create2);
  25. }
  26. }

插入用户信息:

  1. //注册事件 插入user记录
  2. protected void insert_user(user user,SQLiteDatabase sqLiteDatabase){
  3. sqLiteDatabase.execSQL("insert into user_table "
  4. + "(username,password)"
  5. + "values(?,?)" ,
  6. new String[]{user.getUsername(), user.getPassword()}
  7. );
  8. }

匹配用户及密码信息:

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

定义注册登录事件

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

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

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

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

 ContentActivity:

布局xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. android:background="@drawable/background"
  9. android:gravity="center"
  10. tools:context=".ContentActivity">
  11. <TextView
  12. android:id="@+id/textView"
  13. android:layout_width="401dp"
  14. android:layout_height="39dp"
  15. android:text="备忘录"
  16. android:textAlignment="center"
  17. android:textAppearance="@style/TextAppearance.AppCompat.Large" />
  18. <ListView
  19. android:id="@+id/listview"
  20. android:layout_width="398dp"
  21. android:layout_height="613dp" />
  22. <Button
  23. android:id="@+id/button"
  24. android:layout_width="wrap_content"
  25. android:layout_height="65dp"
  26. android:background="@android:drawable/ic_input_add"
  27. android:backgroundTint="#E1D0D0"
  28. app:rippleColor="@color/white" />
  29. </LinearLayout>

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

  1. //查询备忘录并且返回list
  2. protected List<String> queryMemo(String username,SQLiteDatabase db){
  3. memolist.clear();
  4. Cursor cursor = db.rawQuery("select * from content_table where username = ? ", new String[]{username});
  5. while (cursor.moveToNext()){
  6. memolist.add(cursor.getString(2));
  7. }
  8. return memolist;
  9. }
  10. //插入备忘录
  11. protected void insert_title(String username ,String title ,SQLiteDatabase db){
  12. db.execSQL("insert into content_table "
  13. + "(username,memotitle)"
  14. + "values(?,?)" ,
  15. new String[]{username, title}
  16. );
  17. }
  18. //删除备忘录
  19. public void delete_content(String username ,String title ,SQLiteDatabase db){
  20. String sql = "delete from "+ ContentTableName +" where username like '"+username+"' and memotitle like '"+title
  21. +"';";
  22. db.execSQL(sql);
  23. }
  24. //更新备忘录
  25. public void update_content(String username,String old_tit,String new_tit,SQLiteDatabase db){
  26. String sql = "update "+ ContentTableName +" set memotitle = '"+new_tit+"'where username like '"
  27. +username+"' and memotitle like '"+old_tit+"';";
  28. db.execSQL(sql);
  29. }

长按删除数据记录:

  1. //列表点击事件
  2. //长按
  3. listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  4. @Override
  5. public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
  6. final String content = ((TextView) view).getText().toString();
  7. AlertDialog.Builder editDialog = new AlertDialog.Builder(ContentActivity.this);
  8. editDialog.setTitle(content);
  9. editDialog.setMessage("是否删除该备忘录");
  10. editDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  11. @Override
  12. public void onClick(DialogInterface dialogInterface, int i) {
  13. myHelper.delete_content(username, content, db);
  14. Toast.makeText(ContentActivity.this, "数据已删除", Toast.LENGTH_LONG).show();
  15. onResume();
  16. }
  17. });
  18. editDialog.show();
  19. return true;
  20. }
  21. });

短按编辑:

  1. //短按事件
  2. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  3. @Override
  4. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  5. final String content = ((TextView) view).getText().toString();
  6. final EditText editText = new EditText(ContentActivity.this);
  7. editText.setText(memolist.get(i));
  8. AlertDialog.Builder editDialog = new AlertDialog.Builder(ContentActivity.this);
  9. editDialog.setView(editText);
  10. editDialog.setTitle("请重新输入内容:");
  11. editDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  12. @Override
  13. public void onClick(DialogInterface dialogInterface, int i) {
  14. String newtitle = editText.getText().toString();
  15. if (newtitle.length() > 0) {
  16. myHelper.update_content(username, content, newtitle, db);
  17. Toast.makeText(ContentActivity.this, "数据已存入", Toast.LENGTH_LONG).show();
  18. onResume();
  19. } else {
  20. Toast.makeText(ContentActivity.this, "数据为空", Toast.LENGTH_LONG).show();
  21. }
  22. }
  23. });
  24. editDialog.show();
  25. }
  26. });

添加备忘录按钮:

  1. //添加备忘录按键
  2. button = findViewById(R.id.button);
  3. button.setOnClickListener(new View.OnClickListener() {
  4. @Override
  5. public void onClick(View view) {
  6. final EditText editText = new EditText(ContentActivity.this);
  7. AlertDialog.Builder editalertlog = new AlertDialog.Builder(ContentActivity.this);
  8. editalertlog.setTitle("请输入备忘录内容:");
  9. editalertlog.setView(editText);
  10. editalertlog.setPositiveButton("确认", new DialogInterface.OnClickListener() {
  11. @Override
  12. public void onClick(DialogInterface dialogInterface, int i) {
  13. String title = editText.getText().toString();
  14. if(title.length()>0){
  15. myHelper.insert_title(username,title,db);
  16. Toast.makeText(ContentActivity.this,"数据已添加",Toast.LENGTH_LONG).show();
  17. onResume();
  18. }else {
  19. Toast.makeText(ContentActivity.this,"输入数据为空",Toast.LENGTH_LONG).show();
  20. }
  21. }
  22. });
  23. editalertlog.show();
  24. }
  25. });

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

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

  1. @Override
  2. protected void onResume() {
  3. super.onResume();
  4. if (db ==null || !db.isOpen()) {
  5. db = myHelper.getWritableDatabase();
  6. }
  7. memolist = myHelper.queryMemo(username,db);
  8. listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,memolist));
  9. }

全部代码:

MainActivity.java:

  1. ackage com.example.myapplication;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.graphics.drawable.Drawable;
  7. import android.os.Bundle;
  8. import android.text.method.PasswordTransformationMethod;
  9. import android.widget.Button;
  10. import android.widget.CheckBox;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13. public class MainActivity extends AppCompatActivity {
  14. private EditText editText;
  15. private EditText editText1;
  16. Drawable icon;
  17. Drawable icon1;
  18. private CheckBox checkBox;
  19. private MyHelper myHelper;
  20. private SharedPreferences myShare;
  21. private SQLiteDatabase db ;
  22. private Button login;
  23. private Button register;
  24. @Override
  25. protected void onStart() {
  26. super.onStart();
  27. myShare = getSharedPreferences("save_login",MODE_PRIVATE);
  28. Boolean checked = myShare.getBoolean("checkBox",false);
  29. String name = myShare.getString("username",null);
  30. String password = myShare.getString("password",null);
  31. if(checked){
  32. editText.setText("");
  33. editText1.setText("");
  34. }else{
  35. editText.setText(name);
  36. editText1.setText(password);
  37. }
  38. }
  39. @Override
  40. protected void onStop() {
  41. super.onStop();
  42. //myHelper.closeLink();
  43. myShare = getSharedPreferences("save_login",MODE_PRIVATE);
  44. SharedPreferences.Editor myEdit = myShare.edit();
  45. if(checkBox.isChecked()){
  46. myEdit.putString("username",editText.getText().toString());
  47. myEdit.putString("password",editText1.getText().toString());
  48. myEdit.putBoolean("checksave",true);
  49. myEdit.commit();
  50. }else {
  51. myEdit.putBoolean("checksave",false);
  52. myEdit.clear().commit();
  53. }
  54. }
  55. @Override
  56. protected void onCreate(Bundle savedInstanceState) {
  57. super.onCreate(savedInstanceState);
  58. setContentView(R.layout.activity_main1);
  59. editText = findViewById(R.id.loginView);
  60. editText1 = findViewById(R.id.passwordView);
  61. editText1.setTransformationMethod(PasswordTransformationMethod.getInstance());//密码不可见
  62. icon = getResources().getDrawable(R.drawable.profile1);
  63. icon1 = getResources().getDrawable(R.drawable.password);
  64. icon.setBounds(0, 0, 80, 80);
  65. editText.setCompoundDrawables(icon, null, null, null);
  66. icon1.setBounds(0,0,80,80);
  67. editText1.setCompoundDrawables(icon1, null, null, null);
  68. checkBox = findViewById(R.id.checkBox);
  69. myHelper = new MyHelper(MainActivity.this);
  70. db = myHelper.getWritableDatabase();
  71. login = findViewById(R.id.loginbutton);
  72. register = findViewById(R.id.registerbutton);
  73. login.setOnClickListener(view -> {
  74. user user = new user(editText.getText().toString(),editText1.getText().toString());
  75. if (myHelper.queryuser(user,db)){
  76. Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_LONG).show();
  77. Intent intent = new Intent(MainActivity.this,ContentActivity.class);
  78. intent.putExtra("username",editText.getText().toString());
  79. startActivity(intent);
  80. }else {
  81. Toast.makeText(MainActivity.this,"用户名或密码错误",Toast.LENGTH_LONG).show();
  82. }
  83. });
  84. register.setOnClickListener(view -> {
  85. user user = new user(editText.getText().toString(),editText1.getText().toString());
  86. myHelper.insert_user(user,db);
  87. Toast.makeText(MainActivity.this,"注册成功",Toast.LENGTH_LONG).show();
  88. });
  89. }
  90. }

ContentActivity.java

  1. package com.example.myapplication;
  2. import androidx.appcompat.app.AlertDialog;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.AdapterView;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.ListView;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. public class ContentActivity extends AppCompatActivity {
  19. private ListView listView;
  20. private String username;
  21. private String title;
  22. private MyHelper myHelper;
  23. private SQLiteDatabase db;
  24. private List<String> memolist = new ArrayList<>();
  25. private Button button;
  26. private TextView textView;
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_content);
  31. listView = findViewById(R.id.listview);
  32. //接受MainActivity的传值
  33. Intent intent = getIntent();
  34. username = intent.getStringExtra("username");
  35. myHelper =new MyHelper(this);
  36. textView = findViewById(R.id.textView);
  37. textView.setText(username+"的备忘录");
  38. //列表点击事件
  39. //长按
  40. listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  41. @Override
  42. public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
  43. final String content = ((TextView) view).getText().toString();
  44. AlertDialog.Builder editDialog = new AlertDialog.Builder(ContentActivity.this);
  45. editDialog.setTitle(content);
  46. editDialog.setMessage("是否删除该备忘录");
  47. editDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  48. @Override
  49. public void onClick(DialogInterface dialogInterface, int i) {
  50. myHelper.delete_content(username, content, db);
  51. Toast.makeText(ContentActivity.this, "数据已删除", Toast.LENGTH_LONG).show();
  52. onResume();
  53. }
  54. });
  55. editDialog.show();
  56. return true;
  57. }
  58. });
  59. //短按事件
  60. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  61. @Override
  62. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  63. final String content = ((TextView) view).getText().toString();
  64. final EditText editText = new EditText(ContentActivity.this);
  65. editText.setText(memolist.get(i));
  66. AlertDialog.Builder editDialog = new AlertDialog.Builder(ContentActivity.this);
  67. editDialog.setView(editText);
  68. editDialog.setTitle("请重新输入内容:");
  69. editDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  70. @Override
  71. public void onClick(DialogInterface dialogInterface, int i) {
  72. String newtitle = editText.getText().toString();
  73. if (newtitle.length() > 0) {
  74. myHelper.update_content(username, content, newtitle, db);
  75. Toast.makeText(ContentActivity.this, "数据已存入", Toast.LENGTH_LONG).show();
  76. onResume();
  77. } else {
  78. Toast.makeText(ContentActivity.this, "数据为空", Toast.LENGTH_LONG).show();
  79. }
  80. }
  81. });
  82. editDialog.show();
  83. }
  84. });
  85. //添加备忘录按键
  86. button = findViewById(R.id.button);
  87. button.setOnClickListener(new View.OnClickListener() {
  88. @Override
  89. public void onClick(View view) {
  90. final EditText editText = new EditText(ContentActivity.this);
  91. AlertDialog.Builder editalertlog = new AlertDialog.Builder(ContentActivity.this);
  92. editalertlog.setTitle("请输入备忘录内容:");
  93. editalertlog.setView(editText);
  94. editalertlog.setPositiveButton("确认", new DialogInterface.OnClickListener() {
  95. @Override
  96. public void onClick(DialogInterface dialogInterface, int i) {
  97. String title = editText.getText().toString();
  98. if(title.length()>0){
  99. myHelper.insert_title(username,title,db);
  100. Toast.makeText(ContentActivity.this,"数据已添加",Toast.LENGTH_LONG).show();
  101. onResume();
  102. }else {
  103. Toast.makeText(ContentActivity.this,"输入数据为空",Toast.LENGTH_LONG).show();
  104. }
  105. }
  106. });
  107. editalertlog.show();
  108. }
  109. });
  110. }
  111. /**
  112. *
  113. */
  114. @Override
  115. protected void onResume() {
  116. super.onResume();
  117. if (db ==null || !db.isOpen()) {
  118. db = myHelper.getWritableDatabase();
  119. }
  120. memolist = myHelper.queryMemo(username,db);
  121. listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,memolist));
  122. }
  123. /**
  124. *
  125. */
  126. @Override
  127. protected void onStart() {
  128. super.onStart();
  129. }
  130. /**
  131. *
  132. */
  133. @Override
  134. protected void onStop() {
  135. super.onStop();
  136. }
  137. }

Myhelper.java

  1. package com.example.myapplication;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7. import android.widget.Toast;
  8. import androidx.annotation.Nullable;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. public class MyHelper extends SQLiteOpenHelper {
  12. private static final String dbname = "memo.db";
  13. private static final String UserTableName = "user_table";
  14. private static final String ContentTableName = "content_table";
  15. private List<String> memolist = new ArrayList<>();
  16. public MyHelper(@Nullable Context context) {
  17. super(context,dbname, null, 2);
  18. }
  19. @Override
  20. public void onCreate(SQLiteDatabase sqLiteDatabase) {
  21. String sql_create1 = "CREATE TABLE IF NOT EXISTS "+ UserTableName +"("+"_id integer primary key autoincrement,username varchar not null,password varchar not null"+");";
  22. sqLiteDatabase.execSQL(sql_create1);
  23. String sql_create2 = "CREATE TABLE IF NOT EXISTS "+ ContentTableName +"("+"_id integer primary key autoincrement,username varchar not null,memotitle varchar not null"+");";
  24. sqLiteDatabase.execSQL(sql_create2);
  25. }
  26. @Override
  27. public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  28. String drop = "DROP TABLE IF EXISTS "+ UserTableName +";";
  29. sqLiteDatabase.execSQL(drop);
  30. drop = "DROP TABLE IF EXISTS "+ContentTableName +";";
  31. sqLiteDatabase.execSQL(drop);
  32. onCreate(sqLiteDatabase);
  33. }
  34. //查询数据库 先按username 筛选记录 再与password字段匹配 ,如果不存在记录也是返回false
  35. @SuppressLint("Range")
  36. protected boolean queryuser(user user, SQLiteDatabase sqLiteDatabase){
  37. Cursor cursor = sqLiteDatabase.rawQuery("select * from user_table where username = ? and password = ?", new String[]{user.getUsername(), user.getPassword()});
  38. if(cursor.moveToNext()) {
  39. return true;
  40. }
  41. return false;
  42. }
  43. //注册事件 插入user记录
  44. protected void insert_user(user user,SQLiteDatabase sqLiteDatabase){
  45. sqLiteDatabase.execSQL("insert into user_table "
  46. + "(username,password)"
  47. + "values(?,?)" ,
  48. new String[]{user.getUsername(), user.getPassword()}
  49. );
  50. }
  51. //查询备忘录并且返回list
  52. protected List<String> queryMemo(String username,SQLiteDatabase db){
  53. memolist.clear();
  54. Cursor cursor = db.rawQuery("select * from content_table where username = ? ", new String[]{username});
  55. while (cursor.moveToNext()){
  56. memolist.add(cursor.getString(2));
  57. }
  58. return memolist;
  59. }
  60. //插入备忘录
  61. protected void insert_title(String username ,String title ,SQLiteDatabase db){
  62. db.execSQL("insert into content_table "
  63. + "(username,memotitle)"
  64. + "values(?,?)" ,
  65. new String[]{username, title}
  66. );
  67. }
  68. //删除备忘录
  69. public void delete_content(String username ,String title ,SQLiteDatabase db){
  70. String sql = "delete from "+ ContentTableName +" where username like '"+username+"' and memotitle like '"+title
  71. +"';";
  72. db.execSQL(sql);
  73. }
  74. //更新备忘录
  75. public void update_content(String username,String old_tit,String new_tit,SQLiteDatabase db){
  76. String sql = "update "+ ContentTableName +" set memotitle = '"+new_tit+"'where username like '"
  77. +username+"' and memotitle like '"+old_tit+"';";
  78. db.execSQL(sql);
  79. }
  80. }

user.java:

  1. package com.example.myapplication;
  2. public class user {
  3. private String username;
  4. private String password;
  5. public user(String username, String password){
  6. this.username = username;
  7. this.password = password;
  8. }
  9. public String getUsername() {
  10. return username;
  11. }
  12. public String getPassword() {
  13. return password;
  14. }
  15. }

activity_content.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. android:background="@drawable/background"
  9. android:gravity="center"
  10. tools:context=".ContentActivity">
  11. <TextView
  12. android:id="@+id/textView"
  13. android:layout_width="401dp"
  14. android:layout_height="39dp"
  15. android:text="备忘录"
  16. android:textAlignment="center"
  17. android:textAppearance="@style/TextAppearance.AppCompat.Large" />
  18. <ListView
  19. android:id="@+id/listview"
  20. android:layout_width="398dp"
  21. android:layout_height="613dp" />
  22. <Button
  23. android:id="@+id/button"
  24. android:layout_width="wrap_content"
  25. android:layout_height="65dp"
  26. android:background="@android:drawable/ic_input_add"
  27. android:backgroundTint="#E1D0D0"
  28. app:rippleColor="@color/white" />
  29. </LinearLayout>

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="@drawable/img"
  8. tools:context=".MainActivity">
  9. <ImageView
  10. android:id="@+id/imageView"
  11. android:layout_width="122dp"
  12. android:layout_height="109dp"
  13. android:layout_marginStart="152dp"
  14. android:layout_marginBottom="44dp"
  15. android:src="@drawable/userimg"
  16. app:layout_constraintBottom_toTopOf="@+id/loginView"
  17. app:layout_constraintStart_toStartOf="parent" />
  18. <EditText
  19. android:id="@+id/loginView"
  20. android:layout_width="235dp"
  21. android:layout_height="51dp"
  22. android:layout_marginStart="108dp"
  23. android:layout_marginTop="232dp"
  24. android:background="@android:drawable/editbox_background"
  25. android:hint=" Enter your name"
  26. app:layout_constraintStart_toStartOf="parent"
  27. app:layout_constraintTop_toTopOf="parent" />
  28. <EditText
  29. android:id="@+id/passwordView"
  30. android:layout_width="240dp"
  31. android:layout_height="50dp"
  32. android:layout_marginStart="104dp"
  33. android:layout_marginTop="36dp"
  34. android:background="@android:drawable/editbox_background"
  35. android:hint=" Enter your password"
  36. app:layout_constraintStart_toStartOf="parent"
  37. app:layout_constraintTop_toBottomOf="@+id/loginView" />
  38. <Button
  39. android:id="@+id/loginbutton"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_marginStart="84dp"
  43. android:layout_marginTop="144dp"
  44. android:text="登录"
  45. app:layout_constraintStart_toStartOf="parent"
  46. app:layout_constraintTop_toBottomOf="@+id/passwordView" />
  47. <Button
  48. android:id="@+id/registerbutton"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_marginTop="144dp"
  52. android:layout_marginEnd="40dp"
  53. android:text="注册"
  54. app:layout_constraintEnd_toEndOf="parent"
  55. app:layout_constraintTop_toBottomOf="@+id/passwordView" />
  56. <CheckBox
  57. android:id="@+id/checkBox"
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:layout_marginTop="68dp"
  61. android:layout_marginEnd="36dp"
  62. android:text="记住密码"
  63. android:textAppearance="@style/TextAppearance.AppCompat.Medium"
  64. app:layout_constraintEnd_toEndOf="parent"
  65. app:layout_constraintTop_toBottomOf="@+id/passwordView" />
  66. </androidx.constraintlayout.widget.ConstraintLayout>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/475295
推荐阅读
相关标签
  

闽ICP备14008679号