android studio简单的密码日记本,实现增删改查功能

简单的记事本

软件名字:一本日记
软件功能 :简单的密码日记本,实现增删改查功能
软件的使用知识点:

​ 布局方面:线性布局,相对布局

​ 控件方面:常见控件,List View, Navigation View(滑动菜单),FloatingActionButton (悬浮按钮)

​ 后台方面:主要是Adapter和Sq Lite,Intent

项目的结构如下图:

[外链图片转存失败(img-OocaH9L5-1563628277567)(C:\Users\weng\Desktop\安卓期末1.png)]在这里插入图片描述

分析软件的组成:
展示核心的代码
1,登录
        <EditText
             	android:id="@+id/edt_username"------请输入昵称
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginLeft="5dp"
              android:inputType="text"
              android:hint="请输入昵称"
              android:padding="5dp"/>
     	 <EditText
          android:id="@+id/edt_password"----------密 码
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginLeft="5dp"
          android:inputType="textPassword"
          android:hint="请输入密码"
          android:padding="5dp"/>
    	<Button
          android:id="@+id/btn_login"----------登录
          android:layout_below="@+id/line_password"
          android:layout_width="80dp"
          android:layout_marginTop="40dp"
          android:layout_marginLeft="60dp"
          android:text="登 录"
          android:background="@android:color/white"
          android:textSize="24sp"
          android:layout_height="60dp" />
      <Button
          android:id="@+id/btn_go_register"----------注册
          android:layout_below="@+id/line_password"
          android:layout_toRightOf="@+id/btn_login"
          android:layout_width="80dp"
          android:layout_marginTop="40dp"
          android:layout_marginLeft="80dp"
          android:text="注 册"
          android:background="@android:color/white"
          android:textSize="24sp"
          android:layout_height="60dp" />
  
  
 

登录的业务逻辑:

```java
public class LoginActivity extends AppCompatActivity {
    public static int userID;
    public static String username;
    String name, password;//保存数据
    private EditText edt_username;//用户名字
    private EditText edt_password;//用户密码
    private Button btn_login;
    private Button btn_register;
    //private SQLiteDatabase sqlDate;
    private MyDB myDB;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        getSupportActionBar().hide();//隐藏标题栏
        setContentView(R.layout.activity_main);
        initUI();
        myDB = new MyDB(this);//创建对象
        //注册
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegistActivity.class);
                startActivityForResult(intent,1);
            }
        });
        //登入
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                name = edt_username.getText().toString();
                password = edt_password.getText().toString();
                if (enter(name, password)) {
                    Toast.makeText(LoginActivity.this, "登录成功" + name, Toast.LENGTH_SHORT).show();
                    new Thread(){
                        public void run() {
                            try {
                                Thread.sleep(1000);
                                Intent intent1 = new Intent(LoginActivity.this, IndexActivity.class);
                                startActivity(intent1);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }.start();
                } else if (TextUtils.isEmpty(name) || TextUtils.isEmpty(password) || TextUtils.isEmpty(name) && TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this, "请输入用户名或密码", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(LoginActivity.this, "登入失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 初始化:initUI()
     */
    private void initUI() {
        edt_username = (EditText) findViewById(R.id.edt_username);
        edt_password = (EditText) findViewById(R.id.edt_password);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_register = (Button) findViewById(R.id.btn_go_register);
    }
    /**
     * 判断是否username,password 和数据库的相同 enter()
     */
    public boolean enter(String name, String password) {
        SQLiteDatabase db = myDB.getWritableDatabase();
        String sql = "select user_id from tb_user where username=? and password=?";
        Cursor cursor = db.rawQuery(sql, new String[]{name, password});
        if (cursor.getCount() != 0) {//返回Cursor 中的行数
            if (cursor.moveToNext()) {
                userID = cursor.getInt(cursor.getColumnIndex("user_id"));
                username = name;
            }
            cursor.close();
            return true;
        }
        return false;
    }
    /**
     * 数据回显
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1 && resultCode==1){
            String name=data.getStringExtra("name");
            String password=data.getStringExtra("password");
            edt_username.setText(name);
            edt_password.setText(password);
        }
    }
}

2注册

	<EditText
        android:id="@+id/edt_setusername"-------名字
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:inputType="text"
        android:hint="请输入昵称:"
        android:padding="5dp"/>
    <EditText
        android:id="@+id/edt_setpassword"---------密码
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:inputType="textPassword"
        android:hint="请输入密码"
        android:padding="5dp"/>
    <EditText
        android:id="@+id/edt_confirm_password"----确认密码
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:inputType="textPassword"
        android:hint="请输入密码"
        android:padding="5dp"/>
	<Button
    android:id="@+id/btn_register"---------注册
    android:layout_below="@+id/line_confirm_pwd"
    android:layout_width="match_parent"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@android:color/white"
    android:text="确定注册"
    android:textSize="16sp"
    android:layout_height="45dp" />

注册的业务逻辑:

public class RegistActivity extends AppCompatActivity {
    private EditText edt_setname;
    private EditText edt_setpassword;
    private  EditText edt_confirm_password;
    private Button btn_register;
    String name,password,id;
    private MyDB myDB;
    ContentValues contentValues=new ContentValues();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        getSupportActionBar().hide();//隐藏标题栏*/
        setContentView(R.layout.activity_regist);
        initUI();
        myDB=new MyDB(this);
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db=myDB.getWritableDatabase();
                name = edt_setname.getText().toString();
                password = edt_setpassword.getText().toString();
                if (edt_setname.getText().toString().contains(" ") || edt_setpassword.getText().toString().contains(" ")) {
                    Toast.makeText(RegistActivity.this, "输入的用户名或密码不能包含空格", Toast.LENGTH_SHORT).show();
                    return;
                }
                else if(edt_setpassword.getText().toString().length()==0||edt_setname.getText().toString().length()==0){
                    Toast.makeText(RegistActivity.this, "请输入用户名或密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                else if(!edt_confirm_password.getText().toString().equals(edt_setpassword.getText().toString())){
                    Toast.makeText(RegistActivity.this, "两次密码不一致,请确认再注册", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(login(name)){
                    Toast.makeText(RegistActivity.this, "该用户名已注册", Toast.LENGTH_SHORT).show();
                    return;
                }
                contentValues.put("username", name);
                contentValues.put("password", password);
                db.insert("tb_user", null, contentValues);
                String sql="select user_id from tb_user where username=? and password=?";
                Cursor cursor = db.rawQuery(sql, new String[] {name, password});
                if(cursor.moveToNext()){//id
                    id=Integer.toString(cursor.getInt(0));//防止数据库的值null和0 分不清
                }
                cursor.close();
                db.close();
                Toast.makeText(RegistActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        try {
                            Thread.sleep(1000);
                            Intent intent = new Intent(RegistActivity.this,LoginActivity.class);
                            intent.putExtra("name",edt_setname.getText().toString());
                            intent.putExtra("password",edt_setpassword.getText().toString());
                            setResult(1, intent);//返回页面1
                            finish();
                          //  startActivity(intent);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });
    }

    /**
     * 初始化View :initUI()
     */
    public void initUI(){
        edt_setname=(EditText)findViewById(R.id.edt_setusername);
        edt_setpassword=(EditText)findViewById(R.id.edt_setpassword);
        edt_confirm_password=(EditText)findViewById(R.id.edt_confirm_password);
        btn_register=(Button)findViewById(R.id.btn_register);
    }
    /**
     * 查询数据库是否有重复的
     * 进行数据的查询   login()
     */
    public boolean login(String name){
        SQLiteDatabase db=myDB.getWritableDatabase();
        String sql="select * from tb_user where username=?";
        Cursor cursor=db.rawQuery(sql,new String[]{name});
        if (cursor.getCount()!=0){
            cursor.close();
            return true;
        }
        return false;
    }
}

3主页面

<ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    </ListView>
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="30dp"
        android:layout_marginRight="20dp"
        app:fabSize="mini"
        app:srcCompat="@android:drawable/ic_input_add"
        android:id="@+id/floactAdd"
        app:backgroundTint="@color/colorPrimaryDark" />
</RelativeLayout>
<android.support.design.widget.NavigationView
    android:layout_gravity="left"
    app:headerLayout="@layout/head"
    app:menu="@menu/meun_main"
    android:id="@+id/navigat"
    android:layout_width="230dp"
    android:layout_height="match_parent"/>

主页面的业务逻辑:

public class IndexActivity extends AppCompatActivity {
    List<Note> mList;
    private FloatingActionButton fdb;
    private NavigationView navigationView;
    private ListView listView;
    private MyAdapter myAdapter;
    private MyDB myDB;
    private Cursor cursor;
    private SQLiteDatabase dbreader;;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        getSupportActionBar().hide();//隐藏标题栏
        setContentView(R.layout.activity_test);
        listView = (ListView) findViewById(R.id.list);
        navigationView = (NavigationView) findViewById(R.id.navigat);
        View view1 = navigationView.getHeaderView(0);//通过nav来查询head文件
        TextView wc = view1.findViewById(R.id.welcome);
        myDB = new MyDB(this);
        dbreader = myDB.getReadableDatabase();
        wc.setText("欢迎你:" + LoginActivity.username);
        Toast.makeText(IndexActivity.this, "登录成功,欢迎回来!", Toast.LENGTH_SHORT).show();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(IndexActivity.this, showActivity.class);
                intent.putExtra("ID", mList.get(position).getId());
                intent.putExtra("content", mList.get(position).getContent());
                intent.putExtra("time", mList.get(position).getTime());
                startActivity(intent);
        }
        });
        fdb = (FloatingActionButton) findViewById(R.id.floactAdd);
        fdb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(IndexActivity.this, AddContentActivity.class);
                startActivity(intent);
            }
        });
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.prsetposs:
                        Intent intent1=new Intent(IndexActivity.this,setpasswordActivity.class);
                        intent1.putExtra("username",LoginActivity.username);
                        startActivity(intent1);
                        break;
                    case R.id.showtime:
                        Toast.makeText(IndexActivity.this,AddContentActivity.getTime(),Toast.LENGTH_LONG).show();
                        break;
                    case R.id.prsize:
                        Toast.makeText(IndexActivity.this,IndexActivity.this.allDBNum() + "个用户使用了软件",Toast.LENGTH_LONG).show();
                        break;
                    case  R.id.success:
                        Intent intent=new Intent(IndexActivity.this,LoginActivity.class);
                        Toast.makeText(IndexActivity.this,"退出成功",Toast.LENGTH_LONG).show();
                        startActivity(intent);
                }
                return true;
            }
        });
    }
    public void selectDb() {
        mList = new ArrayList<>();
        cursor = dbreader.rawQuery("SELECT * FROM tb_note WHERE userID = ?", new String[]{LoginActivity.userID + ""});
        while (cursor.moveToNext()) {
            Note note = new Note();
            int id = cursor.getInt(cursor.getColumnIndex("ID"));
            String time = cursor.getString(cursor.getColumnIndex("time"));
            String content = cursor.getString(cursor.getColumnIndex("content"));
            note.setTime(time);
            note.setContent(content);
            note.setId(id);
            mList.add(note);
        }
        cursor.close();
        myAdapter = new MyAdapter(this, mList);
        listView.setAdapter(myAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        selectDb();
    }
    /**
     *查询用户的个数 allDBNum()
     */
    public int allDBNum( ){
        String sql = "select count(*) count  from tb_user";
        Cursor cursor = dbreader.rawQuery(sql, null);
         cursor.moveToFirst();
        int count= cursor.getInt(0);
        if (cursor.getCount()!=0){
            cursor.close();
        }
        return count;
    }
}

4 List View 布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="center_vertical"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_content"
        android:textColor="#000"
        android:textSize="20sp"
        android:text="tv"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_time"
        android:textColor="#000"
        android:textSize="20sp"
        android:text="time"/>
</LinearLayout>

5List View适配器:

public class MyAdapter extends BaseAdapter {
    private Context mContext;
    private List<Note> mList;
    private LinearLayout mlayout;

    public MyAdapter(Context mContext, List<Note> mList){
        this.mContext=mContext;
        this.mList=mList;
    }
    @Override
    public int getCount() {
        return mList.size();
    }
    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        mlayout=(LinearLayout) inflater.inflate(R.layout.listre,null);
        TextView content=(TextView)mlayout.findViewById(R.id.list_content);
        TextView time=(TextView)mlayout.findViewById(R.id.list_time);
        content.setText(mList.get(position).getContent());
        time.setText(mList.get(position).getTime());
        return mlayout;
    }
}

6数据库文件:

ublic class MyDB extends SQLiteOpenHelper{
    //用户表
    public static  final  String CREATE_USER="create table tb_user(" +
            "user_id integer primary key autoincrement," +
            "username varchar(50)," +
            "password varchar(50))";
    //
    public static final String CREATE_NOTE="create table tb_note("+
            "ID integer primary key AUTOINCREMENT,"+
            "content TEXT NOT NULL,"+
            "userID  integer,"+
            "time TEXT NOT NULL)";
    //上下文
    private  Context mcontext;
    public static final  String CREATE_NOTES="";
    public MyDB(Context context) {
        super(context, "MyDB.db", null, 2);
        mcontext=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_USER);
        db.execSQL(CREATE_NOTE);
        Toast.makeText(mcontext,"创建成功!",Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        
    }
}

7Note的实体类:

public class Note {
    private Integer id;
    private Integer userID;
    private String content;
    private String time;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUserID() {
        return userID;
    }

    public void setUserID(Integer userID) {
        this.userID = userID;
    }

    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;
    }
}

8添加内容的页面:

<EditText
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="top"
    android:hint="开始记录吧"/>
    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="保存"
        android:onClick="save"/>
    <Button
        android:id="@+id/cancle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="取消"
        android:onClick="cancle"/>

添加内容的业务逻辑:

private EditText met;
private MyDB myDB;
private SQLiteDatabase mysqldb;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
    getSupportActionBar().hide();//隐藏标题栏
    setContentView(R.layout.activity_add_content);
    met=(EditText)findViewById(R.id.text);
    myDB=new MyDB(this);
    mysqldb=myDB.getWritableDatabase();
}
public void save(View v) {
    DdAdd();
    finish();
}
public void cancle(View v) {
    met.setText("");
    finish();
}
public void DdAdd(){
    ContentValues cv = new ContentValues();
    cv.put("content",met.getText().toString());
    cv.put("time",getTime());
    cv.put("userID", LoginActivity.userID);
    mysqldb.insert("tb_note",null,cv);//插入数据
}
public static String getTime(){
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    Date date=new Date();
    String str=sdf.format(date);
    return str;
}

9显示内容的页面:

<TextView
    android:id="@+id/showtime"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"/>
<EditText
    android:id="@+id/showtext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:gravity="top"
    android:layout_weight="1"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:textColor="#000"
    android:hint="记事本"/>
    <Button
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="删除"
        android:onClick="delete"/>
    <Button
        android:id="@+id/update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="修改"
        android:onClick="update" />
    <Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="返回"
        android:onClick="goBack"/>
</LinearLayout>

显示页面的业务逻辑:

public class showActivity extends AppCompatActivity {
    private EditText mtext;
    private TextView time;
    private MyDB myDB;
    private SQLiteDatabase msql;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        getSupportActionBar().hide();//隐藏标题栏
        setContentView(R.layout.activity_show);
        mtext=(EditText) findViewById(R.id.showtext);
        time=(TextView)findViewById(R.id.showtime);
        myDB=new MyDB(this);
        msql=myDB.getWritableDatabase();
        mtext.setInputType(InputType.TYPE_CLASS_TEXT);//开启可以编写
        mtext.setText(getIntent().getStringExtra("content"));
        time.setText(getIntent().getStringExtra("time"));
    }
    /**
     * 删除 delete
     * @param v
     */
    public void delete(View v) {
          int id = getIntent().getIntExtra("ID",0);
          msql.delete("tb_note","ID = " + id,null);
          finish();
          /**
          * 修改 update
          */
    }public void update(View v){
        int id = getIntent().getIntExtra("ID",0);
        String content=mtext.getText().toString();
        msql.execSQL("UPDATE tb_note SET content=? WHERE ID = ?",
                new String[]{content,id+""});
        finish();
    }
    /**
     * 关闭
     * @param v
     */
    public void goBack(View v) {
        finish();
    }

}

10修改密码的页面:

<TextView
    android:layout_marginTop="-120dp"
    android:text="修改密码"
    android:textSize="34dp"
    android:layout_marginBottom="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<EditText
    android:id="@+id/setpass"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="输入新密码"
    />
    <Button
        android:id="@+id/yes"
        android:text="确定"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:onClick="yes"/>
    <Button
        android:id="@+id/no"
        android:text="取消"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:onClick="no"/>
</LinearLayout>

修改密码的业务逻辑:

public class setpasswordActivity extends AppCompatActivity {
    private EditText setpass;
    private MyDB myDB;
    private SQLiteDatabase msql;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        getSupportActionBar().hide();
        setContentView(R.layout.activity_setpassword);
        myDB=new MyDB(this);
        msql=myDB.getWritableDatabase();
        setpass=(EditText)findViewById(R.id.setpass);

    }
    public  void yes(View v){
        String username = getIntent().getStringExtra("username");
        String pass=setpass.getText().toString();
        msql.execSQL("UPDATE  tb_user SET password=? WHERE  username=?",
                new String[]{pass,username});
        Toast.makeText(setpasswordActivity.this, "密码修改完了,请记住现在的密码", Toast.LENGTH_SHORT).show();
        Intent intent2=new Intent(setpasswordActivity.this,LoginActivity.class);
        startActivity(intent2);
        finish();
    }
    public void no(View v){
        Intent intent=new Intent(setpasswordActivity.this,IndexActivity.class);
        startActivity(intent);
        finish();
    }
}
软件的展示图片:

登录:
在这里插入图片描述
注册:

在这里插入图片描述
主页面
在这里插入图片描述
主页面2
在这里插入图片描述
添加
在这里插入图片描述
修改
在这里插入图片描述

  • 20
    点赞
  • 234
    收藏
    觉得还不错? 一键收藏
  • 36
    评论
是的,Android Studio项目可以实现增删改查功能。你可以使用SQLite数据库来存储数据,并使用Java代码来实现增删改查操作。这里是一个简单的示例: 1. 创建一个SQLite数据库和表格来存储数据。 ``` public class DBHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "mydatabase.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_NAME = "mytable"; private static final String COLUMN_ID = "id"; private static final String COLUMN_NAME = "name"; private static final String COLUMN_AGE = "age"; private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME + " TEXT," + COLUMN_AGE + " INTEGER)"; public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } } ``` 2. 实现增删改查操作。 ``` public class MyDAO { private SQLiteDatabase db; private DBHelper dbHelper; public MyDAO(Context context) { dbHelper = new DBHelper(context); db = dbHelper.getWritableDatabase(); } public void addData(String name, int age) { ContentValues values = new ContentValues(); values.put(DBHelper.COLUMN_NAME, name); values.put(DBHelper.COLUMN_AGE, age); db.insert(DBHelper.TABLE_NAME, null, values); } public void deleteData(int id) { db.delete(DBHelper.TABLE_NAME, DBHelper.COLUMN_ID + "=?", new String[]{String.valueOf(id)}); } public void updateData(int id, String name, int age) { ContentValues values = new ContentValues(); values.put(DBHelper.COLUMN_NAME, name); values.put(DBHelper.COLUMN_AGE, age); db.update(DBHelper.TABLE_NAME, values, DBHelper.COLUMN_ID + "=?", new String[]{String.valueOf(id)}); } public List<MyData> getAllData() { List<MyData> dataList = new ArrayList<>(); Cursor cursor = db.query(DBHelper.TABLE_NAME, null, null, null, null, null, null); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_ID)); String name = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_NAME)); int age = cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_AGE)); dataList.add(new MyData(id, name, age)); } return dataList; } } ``` 其中,MyData是一个简单的Java类,用于存储从数据库中读取的数据。 这只是一个简单的示例,实际实现中可能需要更多的代码和复杂的逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 36
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值