Android Studio入门实战--实现数据库增删查改 备忘录实例

目录

要求目的:

效果图:

[Android studio sql 数据库查看工具](#Android studio sql 数据库查看工具)

SQLiteStudio

MainActivity内容:

EditText带icon的布局文件:

[定义Myhepler.java ------用户部分------](#定义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布局文件

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"
    android:background="@drawable/img"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="122dp"
        android:layout_height="109dp"
        android:layout_marginStart="152dp"
        android:layout_marginBottom="44dp"
        android:src="@drawable/userimg"
        app:layout_constraintBottom_toTopOf="@+id/loginView"
        app:layout_constraintStart_toStartOf="parent" />

    <EditText
        android:id="@+id/loginView"
        android:layout_width="235dp"
        android:layout_height="51dp"
        android:layout_marginStart="108dp"
        android:layout_marginTop="232dp"
        android:background="@android:drawable/editbox_background"
        android:hint=" Enter your name"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/passwordView"
        android:layout_width="240dp"
        android:layout_height="50dp"
        android:layout_marginStart="104dp"
        android:layout_marginTop="36dp"
        android:background="@android:drawable/editbox_background"
        android:hint=" Enter your password"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/loginView" />

    <Button
        android:id="@+id/loginbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="84dp"
        android:layout_marginTop="144dp"
        android:text="登录"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/passwordView" />

    <Button
        android:id="@+id/registerbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="144dp"
        android:layout_marginEnd="40dp"
        android:text="注册"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/passwordView" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="68dp"
        android:layout_marginEnd="36dp"
        android:text="记住密码"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/passwordView" />

</androidx.constraintlayout.widget.ConstraintLayout>

icon 文件

java 复制代码
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;

    private  MyHelper myHelper;



    

    @Override
    protected 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

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 final  String UserTableName = "user_table";
    private static  final  String ContentTableName = "content_table";
    private List<String> memolist = new ArrayList<>();

    public MyHelper(@Nullable Context context) {
        super(context,dbname, null, 2);
    }

    @Override
    public 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);

    }
}

插入用户信息:

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

    }

匹配用户及密码信息:

java 复制代码
    //查询数据库 先按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;
    }

定义注册登录事件

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

java 复制代码
  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页面事件:

java 复制代码
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文件:

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:background="@drawable/background"
    android:gravity="center"
    tools:context=".ContentActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="401dp"
        android:layout_height="39dp"
        android:text="备忘录"
        android:textAlignment="center"
        android:textAppearance="@style/TextAppearance.AppCompat.Large" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="398dp"
        android:layout_height="613dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="65dp"
        android:background="@android:drawable/ic_input_add"
        android:backgroundTint="#E1D0D0"
        app:rippleColor="@color/white" />
</LinearLayout>

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

java 复制代码
//查询备忘录并且返回list
    protected  List<String> 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);
    }

长按删除数据记录:

java 复制代码
  //列表点击事件
        //长按
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public 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() {
                    @Override
                    public 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;
            }
        });

短按编辑:

java 复制代码
//短按事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public 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() {
                    @Override
                    public 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();
            }
        });

添加备忘录按钮:

java 复制代码
 //添加备忘录按键
        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public 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() {
                    @Override
                    public 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 需要适配器,才能绑定数据

java 复制代码
@Override
    protected 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:

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;

    private  MyHelper myHelper;


    private SharedPreferences myShare;
    private SQLiteDatabase db ;
    private Button login;
    private Button register;

    @Override
    protected 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);
        }



    }




    @Override
    protected 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();
        }

    }


    @Override
    protected 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

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<String> memolist = new ArrayList<>();
    private Button button;
    private TextView textView;


    @Override
    protected 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() {
            @Override
            public 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() {
                    @Override
                    public 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() {
            @Override
            public 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() {
                    @Override
                    public 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() {
            @Override
            public 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() {
                    @Override
                    public 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();

            }
        });




    }

    /**
     *
     */
    @Override
    protected 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));

    }

    /**
     *
     */
    @Override
    protected void onStart() {
        super.onStart();
    }

    /**
     *
     */
    @Override
    protected void onStop() {
        super.onStop();


    }
}

Myhelper.java

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 final  String UserTableName = "user_table";
    private static  final  String ContentTableName = "content_table";
    private List<String> memolist = new ArrayList<>();

    public MyHelper(@Nullable Context context) {
        super(context,dbname, null, 2);
    }

    @Override
    public 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);

    }



    @Override
    public 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记录
    protected  void insert_user(user user,SQLiteDatabase sqLiteDatabase){
        sqLiteDatabase.execSQL("insert into user_table "
                        + "(username,password)"
                        + "values(?,?)"  ,
                new String[]{user.getUsername(), user.getPassword()}
        );

    }

    //查询备忘录并且返回list
    protected  List<String> 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:

java 复制代码
package com.example.myapplication;

public class user {
    private  String username;
    private  String 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:

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:background="@drawable/background"
    android:gravity="center"
    tools:context=".ContentActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="401dp"
        android:layout_height="39dp"
        android:text="备忘录"
        android:textAlignment="center"
        android:textAppearance="@style/TextAppearance.AppCompat.Large" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="398dp"
        android:layout_height="613dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="65dp"
        android:background="@android:drawable/ic_input_add"
        android:backgroundTint="#E1D0D0"
        app:rippleColor="@color/white" />
</LinearLayout>

activity_main.xml

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"
    android:background="@drawable/img"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="122dp"
        android:layout_height="109dp"
        android:layout_marginStart="152dp"
        android:layout_marginBottom="44dp"
        android:src="@drawable/userimg"
        app:layout_constraintBottom_toTopOf="@+id/loginView"
        app:layout_constraintStart_toStartOf="parent" />

    <EditText
        android:id="@+id/loginView"
        android:layout_width="235dp"
        android:layout_height="51dp"
        android:layout_marginStart="108dp"
        android:layout_marginTop="232dp"
        android:background="@android:drawable/editbox_background"
        android:hint=" Enter your name"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/passwordView"
        android:layout_width="240dp"
        android:layout_height="50dp"
        android:layout_marginStart="104dp"
        android:layout_marginTop="36dp"
        android:background="@android:drawable/editbox_background"
        android:hint=" Enter your password"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/loginView" />

    <Button
        android:id="@+id/loginbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="84dp"
        android:layout_marginTop="144dp"
        android:text="登录"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/passwordView" />

    <Button
        android:id="@+id/registerbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="144dp"
        android:layout_marginEnd="40dp"
        android:text="注册"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/passwordView" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="68dp"
        android:layout_marginEnd="36dp"
        android:text="记住密码"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/passwordView" />

</androidx.constraintlayout.widget.ConstraintLayout>
相关推荐
天天要nx9 分钟前
D64【python 接口自动化学习】- python基础之数据库
数据库·python
精进攻城狮@42 分钟前
Redis(value的数据类型)
数据库·redis
爪哇学长1 小时前
SQL 注入详解:原理、危害与防范措施
xml·java·数据库·sql·oracle
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ1 小时前
mybatisPlus打印sql配置
数据库·sql
弗拉唐1 小时前
将Excel文件的两个表格经过验证后分别读取到Excel表和数据库
数据库·excel
刘艳兵的学习博客2 小时前
刘艳兵-DBA033-如下那种应用场景符合Oracle ROWID存储规则?
服务器·数据库·oracle·面试·刘艳兵
simpleGq2 小时前
Redis知识点整理 - 脑图
数据库·redis·缓存
NiNg_1_2342 小时前
关系型数据库和非关系型数据库详解
数据库·oracle·nosql
paopaokaka_luck2 小时前
基于Spring Boot+Vue的多媒体素材管理系统的设计与实现
java·数据库·vue.js·spring boot·后端·算法
python资深爱好者2 小时前
NoSQL数据库与关系型数据库的主要区别
数据库·oracle·nosql