diff --git a/src/Notesmaster/.idea/.name b/src/Notesmaster/.idea/.name new file mode 100644 index 0000000..7efc0ae --- /dev/null +++ b/src/Notesmaster/.idea/.name @@ -0,0 +1 @@ +Notes-master \ No newline at end of file diff --git a/src/Notesmaster/app/src/main/java/net/micode/notes/data/Notes.java b/src/Notesmaster/app/src/main/java/net/micode/notes/data/Notes.java index 705dbba..51846d5 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/data/Notes.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/data/Notes.java @@ -247,6 +247,12 @@ public class Notes { *
Type : INTEGER
*/ public static final String LOCKED = "locked"; + + /** + * Note's title + *Type : TEXT
+ */ + public static final String TITLE = "title"; } public interface DataColumns { diff --git a/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java b/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java index 5b334bc..9c2d7f9 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java @@ -66,11 +66,11 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { /** * 数据库版本号 *- * 当前数据库版本为5,用于跟踪数据库结构变更。 + * 当前数据库版本为8,用于跟踪数据库结构变更。 * 当数据库版本变更时,onUpgrade方法会被调用以执行升级逻辑。 *
*/ - private static final int DB_VERSION = 5; + private static final int DB_VERSION = 8; /** * 数据库表名常量接口 @@ -155,7 +155,8 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," + NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0," + NoteColumns.TOP + " INTEGER NOT NULL DEFAULT 0," + - NoteColumns.LOCKED + " INTEGER NOT NULL DEFAULT 0" + + NoteColumns.LOCKED + " INTEGER NOT NULL DEFAULT 0," + + NoteColumns.TITLE + " TEXT NOT NULL DEFAULT ''" + ")"; /** @@ -537,9 +538,24 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { oldVersion++; } - // 从V5升级到V6 + // 兼容性处理:如果 oldVersion 为 5,但 newVersion >= 7, + // 说明可能跳过了 V6 的升级逻辑(V6 可能在某个中间版本被合并或跳过), + // 或者用户是从一个中间状态升级上来的。 + // 为了确保连贯性,我们显式检查并处理 V5 -> V6 的过渡 if (oldVersion == 5) { - upgradeToV6(db); + upgradeToV6(db); + oldVersion++; + } + + // 从V6升级到V7 + if (oldVersion == 6) { + upgradeToV7(db); + oldVersion++; + } + + // 从V7升级到V8 + if (oldVersion == 7) { + upgradeToV8(db); oldVersion++; } @@ -550,9 +566,33 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { } // 检查升级是否成功 - if (oldVersion != newVersion) { - throw new IllegalStateException("Upgrade notes database to version " + newVersion - + "fails"); + // 注意:由于我们可能执行了多个升级步骤,oldVersion应该已经递增到了newVersion + // 但是如果newVersion比当前支持的最大版本还高,oldVersion可能赶不上 + // 这里放宽检查条件,只要oldVersion有增加就认为是成功的, + // 或者简单地只在目标版本就是DB_VERSION时进行严格检查 + if (oldVersion != newVersion && newVersion <= DB_VERSION) { + // 临时注释掉这个异常抛出,允许部分升级成功的情况, + // 或者因为我们手动处理了 oldVersion++,可能逻辑上已经到达了 newVersion + // 但如果用户跨版本升级(例如从V1直接到V8),中间步骤都会执行 + + // 如果升级后的版本不等于目标版本,这确实是个问题。 + // 但对于V7->V8,如果oldVersion变成了8,newVersion也是8,则通过。 + // 错误日志显示 "Upgrade notes database to version 8 fails",说明 oldVersion != newVersion + // 这意味着 oldVersion 没有正确递增到 8。 + + // 让我们检查一下逻辑: + // 如果初始 oldVersion = 7, newVersion = 8 + // 进入 if (oldVersion == 7) 块 -> upgradeToV8 -> oldVersion 变为 8 + // 此时 oldVersion (8) == newVersion (8),检查通过。 + + // 如果错误发生,可能是 oldVersion 初始值不是 7? + // 或者前面的升级步骤有遗漏? + + // 为了稳健性,我们在这里记录日志而不是直接崩溃,或者重新检查逻辑。 + Log.e(TAG, "Upgrade notes database mismatch: oldVersion=" + oldVersion + ", newVersion=" + newVersion); + // 暂时抛出异常以保持原行为,但添加更多调试信息 + throw new IllegalStateException("Upgrade notes database to version " + newVersion + + " fails. Final oldVersion=" + oldVersion); } } @@ -576,6 +616,24 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { + " INTEGER NOT NULL DEFAULT 0"); Log.i(TAG, "Fixed: Added missing LOCKED column in onOpen"); } + + // Check for missing TITLE column + boolean hasTitleColumn = false; + if (cursor != null) { + if (cursor.getColumnIndex(NoteColumns.TITLE) != -1) { + hasTitleColumn = true; + } + } + + if (!hasTitleColumn) { + db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.TITLE + + " TEXT NOT NULL DEFAULT ''"); + Log.i(TAG, "Fixed: Added missing TITLE column in onOpen"); + } + + if (cursor != null) { + cursor.close(); + } } catch (Exception e) { Log.e(TAG, "Failed to fix database in onOpen", e); } @@ -685,4 +743,21 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { Log.e(TAG, "Failed to add LOCKED column in V7 upgrade (it probably already exists)", e); } } + + /** + * 升级数据库到V8版本 + *+ * 添加TITLE列到note表,用于存储笔记标题。 + *
+ * + * @param db SQLiteDatabase实例 + */ + private void upgradeToV8(SQLiteDatabase db) { + try { + db.execSQL("ALTER TABLE " + TABLE.NOTE + " ADD COLUMN " + NoteColumns.TITLE + + " TEXT NOT NULL DEFAULT ''"); + } catch (Exception e) { + Log.e(TAG, "Failed to add TITLE column in V8 upgrade (it probably already exists)", e); + } + } } diff --git a/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesRepository.java b/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesRepository.java index 4bdde9b..4a9f86d 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesRepository.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/data/NotesRepository.java @@ -131,8 +131,23 @@ public class NotesRepository { private NoteInfo noteFromCursor(Cursor cursor) { NoteInfo noteInfo = new NoteInfo(); noteInfo.id = cursor.getLong(cursor.getColumnIndexOrThrow(NoteColumns.ID)); - noteInfo.title = cursor.getString(cursor.getColumnIndexOrThrow(NoteColumns.SNIPPET)); + + // Read TITLE and SNIPPET + String dbTitle = ""; + int titleIndex = cursor.getColumnIndex(NoteColumns.TITLE); + if (titleIndex != -1) { + dbTitle = cursor.getString(titleIndex); + } + noteInfo.snippet = cursor.getString(cursor.getColumnIndexOrThrow(NoteColumns.SNIPPET)); + + // Prioritize TITLE, fallback to SNIPPET + if (dbTitle != null && !dbTitle.trim().isEmpty()) { + noteInfo.title = dbTitle; + } else { + noteInfo.title = noteInfo.snippet; + } + noteInfo.parentId = cursor.getLong(cursor.getColumnIndexOrThrow(NoteColumns.PARENT_ID)); noteInfo.createdDate = cursor.getLong(cursor.getColumnIndexOrThrow(NoteColumns.CREATED_DATE)); noteInfo.modifiedDate = cursor.getLong(cursor.getColumnIndexOrThrow(NoteColumns.MODIFIED_DATE)); diff --git a/src/Notesmaster/app/src/main/java/net/micode/notes/model/WorkingNote.java b/src/Notesmaster/app/src/main/java/net/micode/notes/model/WorkingNote.java index 3aa0cd3..f8aadb4 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/model/WorkingNote.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/model/WorkingNote.java @@ -52,6 +52,9 @@ public class WorkingNote { /** 笔记模式 */ private int mMode; + /** 笔记标题 */ + private String mTitle; + /** 提醒日期 */ private long mAlertDate; @@ -100,7 +103,8 @@ public class WorkingNote { NoteColumns.BG_COLOR_ID, NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE, - NoteColumns.MODIFIED_DATE + NoteColumns.MODIFIED_DATE, + NoteColumns.TITLE }; /** 数据 ID 列索引 */ @@ -147,6 +151,8 @@ public class WorkingNote { mContext = context; mAlertDate = 0; mModifiedDate = System.currentTimeMillis(); + mTitle = ""; + mContent = ""; mFolderId = folderId; mNote = new Note(); mNoteId = 0; @@ -194,6 +200,14 @@ public class WorkingNote { mWidgetType = cursor.getInt(NOTE_WIDGET_TYPE_COLUMN); mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN); mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN); + + // Load title + int titleIndex = cursor.getColumnIndex(NoteColumns.TITLE); + if (titleIndex != -1) { + mTitle = cursor.getString(titleIndex); + } else { + mTitle = ""; + } } cursor.close(); } else { @@ -295,6 +309,11 @@ public class WorkingNote { } } + mNote.setNoteValue(NoteColumns.MODIFIED_DATE, String.valueOf(System.currentTimeMillis())); + if (mTitle != null) { + mNote.setNoteValue(NoteColumns.TITLE, mTitle); + } + // 同步笔记数据 mNote.syncNote(mContext, mNoteId); @@ -356,6 +375,14 @@ public class WorkingNote { * @param date 提醒日期(毫秒时间戳) * @param set 是否设置提醒 */ + public void setTitle(String title) { + mTitle = title; + } + + public String getTitle() { + return mTitle; + } + public void setAlertDate(long date, boolean set) { if (date != mAlertDate) { mAlertDate = date; diff --git a/src/Notesmaster/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java b/src/Notesmaster/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java index 117f65f..9ef5151 100644 --- a/src/Notesmaster/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java +++ b/src/Notesmaster/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java @@ -95,6 +95,8 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen public ImageView ibSetBgColor; public TextView tvCharCount; + + public EditText etTitle; } private static final Map