diff --git a/other/个人实践总结/曹馨语.doc b/other/个人实践总结/曹馨语.doc new file mode 100644 index 0000000..e804027 Binary files /dev/null and b/other/个人实践总结/曹馨语.doc differ diff --git a/src/实验二 代码注释/ui/NoteItemData.java b/src/实验二 代码注释/ui/NoteItemData.java new file mode 100644 index 0000000..7764e55 --- /dev/null +++ b/src/实验二 代码注释/ui/NoteItemData.java @@ -0,0 +1,346 @@ +/* + * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// 声明该类所在的包,属于笔记应用的用户界面相关包 +package net.micode.notes.ui; + +import android.content.Context; +import android.database.Cursor; +import android.text.TextUtils; + +import net.micode.notes.data.Contact; +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.tool.DataUtils; + +/** + * NoteItemData 类用于封装从数据库中查询得到的笔记项数据。 + * 它提供了一系列方法来获取笔记的各种属性,如 ID、提醒日期、背景颜色等, + * 同时还处理了笔记在列表中的位置信息,如是否为首个、最后一个、唯一一项等。 + */ +public class NoteItemData { + // 定义从数据库查询笔记数据时需要的列名数组 + static final String [] PROJECTION = new String [] { + NoteColumns.ID, // 笔记的唯一标识符 + NoteColumns.ALERTED_DATE, // 笔记的提醒日期 + NoteColumns.BG_COLOR_ID, // 笔记的背景颜色 ID + NoteColumns.CREATED_DATE, // 笔记的创建日期 + NoteColumns.HAS_ATTACHMENT, // 笔记是否有附件 + NoteColumns.MODIFIED_DATE, // 笔记的修改日期 + NoteColumns.NOTES_COUNT, // 笔记数量 + NoteColumns.PARENT_ID, // 笔记的父文件夹 ID + NoteColumns.SNIPPET, // 笔记的摘要 + NoteColumns.TYPE, // 笔记的类型 + NoteColumns.WIDGET_ID, // 笔记对应的小部件 ID + NoteColumns.WIDGET_TYPE, // 笔记对应的小部件类型 + }; + + // 定义各列在 PROJECTION 数组中的索引位置,方便后续从 Cursor 中获取数据 + private static final int ID_COLUMN = 0; + private static final int ALERTED_DATE_COLUMN = 1; + private static final int BG_COLOR_ID_COLUMN = 2; + private static final int CREATED_DATE_COLUMN = 3; + private static final int HAS_ATTACHMENT_COLUMN = 4; + private static final int MODIFIED_DATE_COLUMN = 5; + private static final int NOTES_COUNT_COLUMN = 6; + private static final int PARENT_ID_COLUMN = 7; + private static final int SNIPPET_COLUMN = 8; + private static final int TYPE_COLUMN = 9; + private static final int WIDGET_ID_COLUMN = 10; + private static final int WIDGET_TYPE_COLUMN = 11; + + // 定义类的成员变量,用于存储从数据库中获取的笔记数据 + private long mId; // 笔记的唯一标识符 + private long mAlertDate; // 笔记的提醒日期 + private int mBgColorId; // 笔记的背景颜色 ID + private long mCreatedDate; // 笔记的创建日期 + private boolean mHasAttachment; // 笔记是否有附件 + private long mModifiedDate; // 笔记的修改日期 + private int mNotesCount; // 笔记数量 + private long mParentId; // 笔记的父文件夹 ID + private String mSnippet; // 笔记的摘要 + private int mType; // 笔记的类型 + private int mWidgetId; // 笔记对应的小部件 ID + private int mWidgetType; // 笔记对应的小部件类型 + private String mName; // 联系人姓名 + private String mPhoneNumber; // 联系人电话号码 + + // 定义用于标记笔记在列表中位置的布尔变量 + private boolean mIsLastItem; // 是否为列表中的最后一项 + private boolean mIsFirstItem; // 是否为列表中的第一项 + private boolean mIsOnlyOneItem; // 是否为列表中唯一的一项 + private boolean mIsOneNoteFollowingFolder; // 是否为文件夹后的单条笔记 + private boolean mIsMultiNotesFollowingFolder; // 是否为文件夹后的多条笔记 + + /** + * 构造函数,用于从 Cursor 中获取笔记数据并初始化类的成员变量。 + * @param context 上下文对象,用于获取 ContentResolver 等系统服务 + * @param cursor 包含笔记数据的 Cursor 对象 + */ + public NoteItemData(Context context, Cursor cursor) { + // 从 Cursor 中获取各列的数据并赋值给对应的成员变量 + mId = cursor.getLong(ID_COLUMN); + mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN); + mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN); + mCreatedDate = cursor.getLong(CREATED_DATE_COLUMN); + mHasAttachment = (cursor.getInt(HAS_ATTACHMENT_COLUMN) > 0) ? true : false; + mModifiedDate = cursor.getLong(MODIFIED_DATE_COLUMN); + mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN); + mParentId = cursor.getLong(PARENT_ID_COLUMN); + mSnippet = cursor.getString(SNIPPET_COLUMN); + // 去除摘要中的特定标签 + mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace( + NoteEditActivity.TAG_UNCHECKED, ""); + mType = cursor.getInt(TYPE_COLUMN); + mWidgetId = cursor.getInt(WIDGET_ID_COLUMN); + mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN); + + mPhoneNumber = ""; + // 如果笔记的父文件夹 ID 为通话记录文件夹的 ID + if (mParentId == Notes.ID_CALL_RECORD_FOLDER) { + // 通过笔记 ID 获取通话号码 + mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId); + if (!TextUtils.isEmpty(mPhoneNumber)) { + // 根据电话号码获取联系人姓名 + mName = Contact.getContact(context, mPhoneNumber); + if (mName == null) { + // 如果未找到联系人姓名,则使用电话号码作为姓名 + mName = mPhoneNumber; + } + } + } + + if (mName == null) { + mName = ""; + } + // 检查笔记在列表中的位置信息 + checkPostion(cursor); + } + + /** + * 检查笔记在 Cursor 中的位置信息,设置相应的布尔标记。 + * @param cursor 包含笔记数据的 Cursor 对象 + */ + private void checkPostion(Cursor cursor) { + // 判断是否为列表中的最后一项 + mIsLastItem = cursor.isLast() ? true : false; + // 判断是否为列表中的第一项 + mIsFirstItem = cursor.isFirst() ? true : false; + // 判断是否为列表中唯一的一项 + mIsOnlyOneItem = (cursor.getCount() == 1); + mIsMultiNotesFollowingFolder = false; + mIsOneNoteFollowingFolder = false; + + // 如果笔记类型为普通笔记且不是第一项 + if (mType == Notes.TYPE_NOTE && !mIsFirstItem) { + int position = cursor.getPosition(); + if (cursor.moveToPrevious()) { + // 如果前一项是文件夹或系统类型 + if (cursor.getInt(TYPE_COLUMN) == Notes.TYPE_FOLDER + || cursor.getInt(TYPE_COLUMN) == Notes.TYPE_SYSTEM) { + if (cursor.getCount() > (position + 1)) { + // 如果后面还有其他笔记,则标记为文件夹后的多条笔记 + mIsMultiNotesFollowingFolder = true; + } else { + // 否则标记为文件夹后的单条笔记 + mIsOneNoteFollowingFolder = true; + } + } + // 将 Cursor 移动回原来的位置 + if (!cursor.moveToNext()) { + throw new IllegalStateException("cursor move to previous but can't move back"); + } + } + } + } + + /** + * 判断是否为文件夹后的单条笔记。 + * @return 如果是则返回 true,否则返回 false + */ + public boolean isOneFollowingFolder() { + return mIsOneNoteFollowingFolder; + } + + /** + * 判断是否为文件夹后的多条笔记。 + * @return 如果是则返回 true,否则返回 false + */ + public boolean isMultiFollowingFolder() { + return mIsMultiNotesFollowingFolder; + } + + /** + * 判断是否为列表中的最后一项。 + * @return 如果是则返回 true,否则返回 false + */ + public boolean isLast() { + return mIsLastItem; + } + + /** + * 获取通话记录对应的联系人姓名。 + * @return 联系人姓名 + */ + public String getCallName() { + return mName; + } + + /** + * 判断是否为列表中的第一项。 + * @return 如果是则返回 true,否则返回 false + */ + public boolean isFirst() { + return mIsFirstItem; + } + + /** + * 判断是否为列表中唯一的一项。 + * @return 如果是则返回 true,否则返回 false + */ + public boolean isSingle() { + return mIsOnlyOneItem; + } + + /** + * 获取笔记的唯一标识符。 + * @return 笔记的 ID + */ + public long getId() { + return mId; + } + + /** + * 获取笔记的提醒日期。 + * @return 提醒日期 + */ + public long getAlertDate() { + return mAlertDate; + } + + /** + * 获取笔记的创建日期。 + * @return 创建日期 + */ + public long getCreatedDate() { + return mCreatedDate; + } + + /** + * 判断笔记是否有附件。 + * @return 如果有则返回 true,否则返回 false + */ + public boolean hasAttachment() { + return mHasAttachment; + } + + /** + * 获取笔记的修改日期。 + * @return 修改日期 + */ + public long getModifiedDate() { + return mModifiedDate; + } + + /** + * 获取笔记的背景颜色 ID。 + * @return 背景颜色 ID + */ + public int getBgColorId() { + return mBgColorId; + } + + /** + * 获取笔记的父文件夹 ID。 + * @return 父文件夹 ID + */ + public long getParentId() { + return mParentId; + } + + /** + * 获取笔记数量。 + * @return 笔记数量 + */ + public int getNotesCount() { + return mNotesCount; + } + + /** + * 获取笔记所在的文件夹 ID,与 getParentId 功能相同。 + * @return 文件夹 ID + */ + public long getFolderId () { + return mParentId; + } + + /** + * 获取笔记的类型。 + * @return 笔记类型 + */ + public int getType() { + return mType; + } + + /** + * 获取笔记对应的小部件类型。 + * @return 小部件类型 + */ + public int getWidgetType() { + return mWidgetType; + } + + /** + * 获取笔记对应的小部件 ID。 + * @return 小部件 ID + */ + public int getWidgetId() { + return mWidgetId; + } + + /** + * 获取笔记的摘要。 + * @return 笔记摘要 + */ + public String getSnippet() { + return mSnippet; + } + + /** + * 判断笔记是否设置了提醒。 + * @return 如果设置了提醒则返回 true,否则返回 false + */ + public boolean hasAlert() { + return (mAlertDate > 0); + } + + /** + * 判断笔记是否为通话记录。 + * @return 如果是通话记录则返回 true,否则返回 false + */ + public boolean isCallRecord() { + return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber)); + } + + /** + * 从 Cursor 中获取笔记的类型。 + * @param cursor 包含笔记数据的 Cursor 对象 + * @return 笔记类型 + */ + public static int getNoteType(Cursor cursor) { + return cursor.getInt(TYPE_COLUMN); + } +} \ No newline at end of file