Android studio源码记事本可做备忘录 纯安卓项目本地数据库sqlite 功能登陆注册/增删改/注销 项目包含 源码apk文件演示视频 温馨提示~ 由于项目具有复制性售出不退不包售后 项目直接导入即可运行推荐Android studio版本大于3.5运行手机版本9或10 源码可确保无任何问题最近搞了个实用的小玩意儿——基于Android Studio的本地备忘录项目麻雀虽小但登录注册、增删改查这些基本功都齐活。直接上硬货看看怎么用SQLite在安卓里玩转数据存储。数据库搭建是基本功直接继承SQLiteOpenHelper搞事情public class DBHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME memo.db; private static final int DATABASE_VERSION 2; // 用户表 private static final String CREATE_USER_TABLE CREATE TABLE users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE, password TEXT); // 备忘录表 private static final String CREATE_MEMO_TABLE CREATE TABLE memos ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, content TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP); public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_USER_TABLE); db.execSQL(CREATE_MEMO_TABLE); } }这里整了两个表用户表和备忘录表通过userid关联。注意DATETIME字段用了CURRENTTIMESTAMP自动记录创建时间省得手动处理时间戳。注册登录这活儿用到了SQLite的CRUD基础操作。来看个用户注册的骚操作public boolean registerUser(String username, String password) { SQLiteDatabase db this.getWritableDatabase(); ContentValues values new ContentValues(); values.put(username, username); values.put(password, password); // 实际项目记得加密 long result db.insert(users, null, values); return result ! -1; }重点在db.insert方法返回的long值-1表示翻车。实际项目里密码肯定要加盐哈希这里为了演示简化了。备忘录的核心操作得配上事务处理才稳public void addMemo(int userId, String content) { SQLiteDatabase db this.getWritableDatabase(); db.beginTransaction(); try { ContentValues values new ContentValues(); values.put(user_id, userId); values.put(content, content); db.insertOrThrow(memos, null, values); db.setTransactionSuccessful(); } catch (Exception e) { Log.e(DB_ERROR, 添加备忘失败, e); } finally { db.endTransaction(); } }这里用了insertOrThrow出错直接抛异常。加上事务三件套beginTransaction、setTransactionSuccessful、endTransaction确保操作原子性。Android studio源码记事本可做备忘录 纯安卓项目本地数据库sqlite 功能登陆注册/增删改/注销 项目包含 源码apk文件演示视频 温馨提示~ 由于项目具有复制性售出不退不包售后 项目直接导入即可运行推荐Android studio版本大于3.5运行手机版本9或10 源码可确保无任何问题数据展示方面CursorAdapter和RecyclerView.Adapter是绝配。看个精简版适配器public class MemoAdapter extends RecyclerView.AdapterMemoViewHolder { private Cursor cursor; public void swapCursor(Cursor newCursor) { if (cursor ! null) cursor.close(); cursor newCursor; notifyDataSetChanged(); } Override public void onBindViewHolder(MemoViewHolder holder, int position) { cursor.moveToPosition(position); String content cursor.getString( cursor.getColumnIndexOrThrow(content)); holder.tvContent.setText(content); } }用swapCursor方法实现数据更新记得及时关闭旧Cursor防止内存泄漏。Cursor直接绑到RecyclerView上数据变化自动刷新。项目里还藏了个小彩蛋——时间轴展示。利用SQLite的日期函数搞事情public Cursor getMemosByDate(int userId, String date) { String query SELECT strftime(%H:%M, created_at) as time, content FROM memos WHERE user_id ? AND date(created_at) ? ORDER BY created_at DESC; return db.rawQuery(query, new String[]{String.valueOf(userId), date}); }strftime函数直接格式化时间配合date()函数按天筛选。这种SQL层面的处理比在Java里操作高效得多。源码扔到Android Studio直接能跑建议用Pixel 3 API 29的模拟器数据持久化表现最稳定。遇到构建问题大概率是Gradle版本不对改改gradle-wrapper.properties里的distributionUrl就能搞定。整个项目走的是极简路线但该有的数据关联、事务处理、UI绑定一个不少。拿来练手二次开发正合适加个云同步或者分类标签直接能当毕业设计项目使。