diff --git a/src/ui(r)/NoteItemData.java b/src/ui(r)/NoteItemData.java index 09c4e0e..9baf5b4 100644 --- a/src/ui(r)/NoteItemData.java +++ b/src/ui(r)/NoteItemData.java @@ -1,140 +1,175 @@ /* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) - * + * 这行注释表明版权所有者是The MiCode Open Source Community,时间为2010-2011年。 + */ +/* * Licensed under the Apache License, Version 2.0 (the "License"); + * 这行注释说明该文件是在Apache License 2.0下授权的。 + */ +/* * 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 - * + * 你可以通过以下网址获得许可证的副本: + */ +/* + * http://www.apache.org/licenses/LICENSE-2.0 + * 这是Apache 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; +// 指定包名为net.micode.notes.ui。 import android.content.Context; +// 导入Context类,用于访问应用程序的环境信息。 + import android.database.Cursor; +// 导入Cursor类,用于遍历数据库查询结果。 + import android.text.TextUtils; +// 导入TextUtils类,用于操作文本工具。 import net.micode.notes.data.Contact; +// 导入Contact类,用于处理联系人数据。 + import net.micode.notes.data.Notes; +// 导入Notes类,用于处理便签数据。 + import net.micode.notes.data.Notes.NoteColumns; +// 导入NoteColumns类,用于访问便签数据的列。 + import net.micode.notes.tool.DataUtils; +// 导入DataUtils类,用于数据处理工具。 // NoteItemData类用于封装便签项的数据 public class NoteItemData { // 数据库查询的列 static final String [] PROJECTION = new String [] { - NoteColumns.ID, - NoteColumns.ALERTED_DATE, - NoteColumns.BG_COLOR_ID, - NoteColumns.CREATED_DATE, - NoteColumns.HAS_ATTACHMENT, - NoteColumns.MODIFIED_DATE, - NoteColumns.NOTES_COUNT, - NoteColumns.PARENT_ID, - NoteColumns.SNIPPET, - NoteColumns.TYPE, - NoteColumns.WIDGET_ID, - NoteColumns.WIDGET_TYPE, + NoteColumns.ID, // 便签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, // 便签小部件类型 }; // 列索引 - 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 static final int ID_COLUMN = 0; // ID列索引 + private static final int ALERTED_DATE_COLUMN = 1; // ALERTED_DATE列索引 + private static final int BG_COLOR_ID_COLUMN = 2; // BG_COLOR_ID列索引 + private static final int CREATED_DATE_COLUMN = 3; // CREATED_DATE列索引 + private static final int HAS_ATTACHMENT_COLUMN = 4; // HAS_ATTACHMENT列索引 + private static final int MODIFIED_DATE_COLUMN = 5; // MODIFIED_DATE列索引 + private static final int NOTES_COUNT_COLUMN = 6; // NOTES_COUNT列索引 + private static final int PARENT_ID_COLUMN = 7; // PARENT_ID列索引 + private static final int SNIPPET_COLUMN = 8; // SNIPPET列索引 + private static final int TYPE_COLUMN = 9; // TYPE列索引 + private static final int WIDGET_ID_COLUMN = 10; // WIDGET_ID列索引 + private static final int WIDGET_TYPE_COLUMN = 11; // WIDGET_TYPE列索引 // 成员变量,存储便签项的数据 - private long mId; - private long mAlertDate; - private int mBgColorId; - private long mCreatedDate; - private boolean mHasAttachment; - private long mModifiedDate; - private int mNotesCount; - private long mParentId; - private String mSnippet; - private int mType; - private int mWidgetId; - private int mWidgetType; - private String mName; - private String mPhoneNumber; + private long mId; // 便签ID + 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; + private boolean mIsLastItem; // 是否是最后一项 + private boolean mIsFirstItem; // 是否是第一项 + private boolean mIsOnlyOneItem; // 是否只有一项 + private boolean mIsOneNoteFollowingFolder; // 是否有一个便签项跟随文件夹 + private boolean mIsMultiNotesFollowingFolder; // 是否有多个便签项跟随文件夹 // 构造函数,从游标中初始化便签项的数据 public NoteItemData(Context context, 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); - + // 从游标中获取数据并赋值给成员变量 + mId = cursor.getLong(ID_COLUMN); // 获取便签ID + mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN); // 获取便签提醒日期 + mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN); // 获取便签背景色ID + 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); // 获取便签的父ID + 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); // 获取便签小部件ID + mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN); // 获取便签小部件类型 + + // 如果便签项是通话记录文件夹 mPhoneNumber = ""; if (mParentId == Notes.ID_CALL_RECORD_FOLDER) { - mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId); + mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId); // 获取通话记录号码 if (!TextUtils.isEmpty(mPhoneNumber)) { - mName = Contact.getContact(context, mPhoneNumber); + mName = Contact.getContact(context, mPhoneNumber); // 获取联系人名称 if (mName == null) { - mName = mPhoneNumber; + mName = mPhoneNumber; // 如果联系人名称为空,则使用电话号码 } } } if (mName == null) { - mName = ""; + mName = ""; // 如果联系人名称为空,则设置为空字符串 } - checkPostion(cursor); + checkPostion(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) { + // 设置便签项的位置状态标志 + 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; + mIsMultiNotesFollowingFolder = true; // 有多个便签项跟随文件夹 } else { - mIsOneNoteFollowingFolder = true; + mIsOneNoteFollowingFolder = true; // 有单个便签项跟随文件夹 } } - if (!cursor.moveToNext()) { - throw new IllegalStateException("cursor move to previous but can't move back"); + if (!cursor.moveToNext()) { // 移动回原来的位置 + throw new IllegalStateException("cursor move to previous but can't move back"); // 如果无法移动回原来的位置,抛出异常 } } } @@ -142,90 +177,90 @@ public class NoteItemData { // 获取便签项的位置状态 public boolean isOneFollowingFolder() { - return mIsOneNoteFollowingFolder; + return mIsOneNoteFollowingFolder; // 返回是否有单个便签项跟随文件夹 } public boolean isMultiFollowingFolder() { - return mIsMultiNotesFollowingFolder; + return mIsMultiNotesFollowingFolder; // 返回是否有多个便签项跟随文件夹 } public boolean isLast() { - return mIsLastItem; + return mIsLastItem; // 返回是否是最后一项 } public String getCallName() { - return mName; + return mName; // 返回联系人名称 } public boolean isFirst() { - return mIsFirstItem; + return mIsFirstItem; // 返回是否是第一项 } public boolean isSingle() { - return mIsOnlyOneItem; + return mIsOnlyOneItem; // 返回是否只有一项 } public long getId() { - return mId; + return mId; // 返回便签ID } public long getAlertDate() { - return mAlertDate; + return mAlertDate; // 返回便签提醒日期 } public long getCreatedDate() { - return mCreatedDate; + return mCreatedDate; // 返回便签创建日期 } public boolean hasAttachment() { - return mHasAttachment; + return mHasAttachment; // 返回便签是否有附件 } public long getModifiedDate() { - return mModifiedDate; + return mModifiedDate; // 返回便签修改日期 } public int getBgColorId() { - return mBgColorId; + return mBgColorId; // 返回便签背景色ID } public long getParentId() { - return mParentId; + return mParentId; // 返回便签的父ID } public int getNotesCount() { - return mNotesCount; + return mNotesCount; // 返回便签数量 } - public long getFolderId () { - return mParentId; + public long getFolderId() { + return mParentId; // 返回便签文件夹ID,与父ID相同 } public int getType() { - return mType; + return mType; // 返回便签类型 } public int getWidgetType() { - return mWidgetType; + return mWidgetType; // 返回便签小部件类型 } public int getWidgetId() { - return mWidgetId; + return mWidgetId; // 返回便签小部件ID } public String getSnippet() { - return mSnippet; + return mSnippet; // 返回便签摘要 } public boolean hasAlert() { - return (mAlertDate > 0); + return (mAlertDate > 0); // 返回便签是否有提醒,即提醒日期是否大于0 } public boolean isCallRecord() { - return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber)); + return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber)); // 返回是否是通话记录 } public static int getNoteType(Cursor cursor) { - return cursor.getInt(TYPE_COLUMN); + return cursor.getInt(TYPE_COLUMN); // 根据游标获取便签类型 } } \ No newline at end of file diff --git a/src/ui(r)/NotesListAdapter.java b/src/ui(r)/NotesListAdapter.java index c2c3a1e..67600e3 100644 --- a/src/ui(r)/NotesListAdapter.java +++ b/src/ui(r)/NotesListAdapter.java @@ -1,196 +1,238 @@ /* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) - * + * 版权所有者为The MiCode Open Source Community,时间为2010-2011年。 + */ +/* * Licensed under the Apache License, Version 2.0 (the "License"); + * 该文件在Apache License 2.0下授权,"许可证"指的就是这个版本。 + */ +/* * 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 - * + * 你可以在以下网址获得许可证的副本: + */ +/* + * http://www.apache.org/licenses/LICENSE-2.0 + * Apache 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; +// 定义包名为net.micode.notes.ui。 import android.content.Context; +// 导入Context类,用于获取应用程序的环境信息。 + import android.database.Cursor; +// 导入Cursor类,用于遍历数据库查询结果。 + import android.util.Log; +// 导入Log类,用于打印日志信息。 + import android.view.View; +// 导入View类,用于创建和管理视图。 + import android.view.ViewGroup; +// 导入ViewGroup类,用于管理布局中的视图组。 + import android.widget.CursorAdapter; +// 导入CursorAdapter类,用于将游标中的数据绑定到视图上。 import net.micode.notes.data.Notes; +// 导入Notes类,用于操作便签数据。 import java.util.Collection; +// 导入Collection接口,用于处理集合。 + import java.util.HashMap; +// 导入HashMap类,用于存储键值对。 + import java.util.HashSet; +// 导入HashSet类,用于存储不重复的元素集合。 + import java.util.Iterator; +// 导入Iterator接口,用于遍历集合。 // NotesListAdapter是CursorAdapter的子类,用于为便签列表提供数据和视图 public class NotesListAdapter extends CursorAdapter { private static final String TAG = "NotesListAdapter"; - private Context mContext; // 上下文对象 + // 定义TAG常量,用于日志输出。 + + private Context mContext; // 存储上下文对象 private HashMap mSelectedIndex; // 存储选中状态的哈希图 - private int mNotesCount; // 便签数量 - private boolean mChoiceMode; // 选择模式标志 + private int mNotesCount; // 存储便签数量 + private boolean mChoiceMode; // 标记是否处于选择模式 // AppWidgetAttribute内部类,用于存储小部件的属性 public static class AppWidgetAttribute { - public int widgetId; - public int widgetType; + public int widgetId; // 小部件ID + public int widgetType; // 小部件类型 }; - // 构造函数 + // NotesListAdapter的构造函数 public NotesListAdapter(Context context) { - super(context, null); - mSelectedIndex = new HashMap(); - mContext = context; - mNotesCount = 0; + super(context, null); // 调用父类构造函数 + mSelectedIndex = new HashMap(); // 初始化选中状态哈希图 + mContext = context; // 保存上下文对象 + mNotesCount = 0; // 初始化便签数量 } - // 新建视图 + // 新建视图的方法,用于创建新的列表项视图 @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { - return new NotesListItem(context); + return new NotesListItem(context); // 返回一个新的NotesListItem视图 } - // 绑定视图和数据 + // 绑定视图和数据的方法,用于将游标中的数据绑定到视图上 @Override public void bindView(View view, Context context, Cursor cursor) { if (view instanceof NotesListItem) { - NoteItemData itemData = new NoteItemData(context, cursor); - ((NotesListItem) view).bind(context, itemData, mChoiceMode, - isSelectedItem(cursor.getPosition())); + NoteItemData itemData = new NoteItemData(context, cursor); // 创建NoteItemData对象 + ((NotesListItem) view).bind(context, itemData, mChoiceMode, // 绑定数据到视图 + isSelectedItem(cursor.getPosition())); // 传递选择状态 } } - // 设置选中项 + // 设置选中项的方法,用于改变项的选中状态 public void setCheckedItem(final int position, final boolean checked) { - mSelectedIndex.put(position, checked); - notifyDataSetChanged(); + mSelectedIndex.put(position, checked); // 更新选中状态 + notifyDataSetChanged(); // 通知数据改变 } - // 获取选择模式 + // 获取选择模式的方法,用于判断是否处于选择模式 public boolean isInChoiceMode() { - return mChoiceMode; + return mChoiceMode; // 返回选择模式标志 } - // 设置选择模式 + // 设置选择模式的方法,用于设置选择模式 public void setChoiceMode(boolean mode) { - mSelectedIndex.clear(); - mChoiceMode = mode; + mSelectedIndex.clear(); // 清除选中状态 + mChoiceMode = mode; // 设置选择模式标志 } - // 全选或全不选 + // 全选或全不选的方法,用于设置所有项的选中状态 public void selectAll(boolean checked) { - Cursor cursor = getCursor(); + Cursor cursor = getCursor(); // 获取游标 for (int i = 0; i < getCount(); i++) { - if (cursor.moveToPosition(i)) { - if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { - setCheckedItem(i, checked); + if (cursor.moveToPosition(i)) { // 移动到指定位置 + if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { // 如果是便签类型 + setCheckedItem(i, checked); // 设置选中状态 } } } } - // 获取选中的便签ID集合 + // 获取选中的便签ID集合的方法,用于获取所有选中项的ID public HashSet getSelectedItemIds() { - HashSet itemSet = new HashSet(); - for (Integer position : mSelectedIndex.keySet()) { - if (mSelectedIndex.get(position) == true) { - Long id = getItemId(position); - if (id == Notes.ID_ROOT_FOLDER) { - Log.d(TAG, "Wrong item id, should not happen"); + HashSet itemSet = new HashSet(); // 创建ID集合 + for (Integer position : mSelectedIndex.keySet()) { // 遍历选中状态集合 + if (mSelectedIndex.get(position) == true) { // 如果项被选中 + Long id = getItemId(position); // 获取项的ID + if (id == Notes.ID_ROOT_FOLDER) { // 如果ID是根文件夹ID + Log.d(TAG, "Wrong item id, should not happen"); // 打印错误日志 } else { - itemSet.add(id); + itemSet.add(id); // 添加ID到集合 } } } - - return itemSet; + return itemSet; // 返回ID集合 } - // 获取选中的小部件属性集合 + // 获取选中的小部件属性集合的方法,用于获取所有选中小部件的属性 public HashSet getSelectedWidget() { - HashSet itemSet = new HashSet(); - for (Integer position : mSelectedIndex.keySet()) { - if (mSelectedIndex.get(position) == true) { - Cursor c = (Cursor) getItem(position); - if (c != null) { - AppWidgetAttribute widget = new AppWidgetAttribute(); - NoteItemData item = new NoteItemData(mContext, c); - widget.widgetId = item.getWidgetId(); - widget.widgetType = item.getWidgetType(); - itemSet.add(widget); + HashSet itemSet = new HashSet(); // 创建小部件属性集合 + for (Integer position : mSelectedIndex.keySet()) { // 遍历选中状态集合 + if (mSelectedIndex.get(position) == true) { // 如果项被选中 + Cursor c = (Cursor) getItem(position); // 获取游标项 + if (c != null) { // 如果游标项不为空 + AppWidgetAttribute widget = new AppWidgetAttribute(); // 创建小部件属性对象 + NoteItemData item = new NoteItemData(mContext, c); // 创建NoteItemData对象 + widget.widgetId = item.getWidgetId(); // 获取小部件ID + widget.widgetType = item.getWidgetType(); // 获取小部件类型 + itemSet.add(widget); // 添加小部件属性到集合 } else { - Log.e(TAG, "Invalid cursor"); - return null; + Log.e(TAG, "Invalid cursor"); // 打印错误日志 + return null; // 返回null } } } - return itemSet; + return itemSet; // 返回小部件属性集合 } - // 获取选中数量 + // 获取选中数量的方法,用于计算选中项的数量 public int getSelectedCount() { - Collection values = mSelectedIndex.values(); + Collection values = mSelectedIndex.values(); // 获取选中状态值集合 if (null == values) { - return 0; + return 0; // 如果集合为空,返回0 } - Iterator iter = values.iterator(); - int count = 0; - while (iter.hasNext()) { - if (true == iter.next()) { - count++; + Iterator iter = values.iterator(); // 创建迭代器 + int count = 0; // 初始化计数器 + while (iter.hasNext()) { // 遍历集合 + if (true == iter.next()) { // 如果项被选中 + count++; // 增加计数器 } } - return count; + return count; // 返回选中数量 } - // 检查是否全部选中 + // 检查是否全部选中的方法,用于判断所有项是否被选中 public boolean isAllSelected() { - int checkedCount = getSelectedCount(); - return (checkedCount != 0 && checkedCount == mNotesCount); + int checkedCount = getSelectedCount(); // 获取选中数量 + return (checkedCount != 0 && checkedCount == mNotesCount); // 如果选中数量不等于0且等于便签数量,则返回true } - // 检查指定位置的项是否被选中 + // 检查指定位置的项是否被选中的方法,用于判断特定项的选中状态 public boolean isSelectedItem(final int position) { - if (null == mSelectedIndex.get(position)) { - return false; + if (null == mSelectedIndex.get(position)) { // 如果项的选中状态为null + return false; // 则返回false } - return mSelectedIndex.get(position); + return mSelectedIndex.get(position); // 否则返回选中状态 } - // 当内容变化时调用 + // 当内容变化时调用的方法,用于更新便签数量 @Override protected void onContentChanged() { - super.onContentChanged(); - calcNotesCount(); + super.onContentChanged(); // 调用父类方法 + calcNotesCount(); // 计算便签数量 } - // 更改游标时调用 + // 更改游标时调用的方法,用于更新便签数量 @Override public void changeCursor(Cursor cursor) { - super.changeCursor(cursor); - calcNotesCount(); + super.changeCursor(cursor); // 调用父类方法 + calcNotesCount(); // 计算便签数量 } - // 计算便签数量 + // 计算便签数量的方法,用于计算游标中的便签数量 private void calcNotesCount() { - mNotesCount = 0; - for (int i = 0; i < getCount(); i++) { - Cursor c = (Cursor) getItem(i); - if (c != null) { - if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) { - mNotesCount++; + mNotesCount = 0; // 初始化便签数量 + for (int i = 0; i < getCount(); i++) { // 遍历游标 + Cursor c = (Cursor) getItem(i); // 获取游标项 + if (c != null) { // 如果游标项不为空 + if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) { // 如果是便签类型 + mNotesCount++; // 增加便签数量 } } else { - Log.e(TAG, "Invalid cursor"); - return; + Log.e(TAG, "Invalid cursor"); // 打印错误日志 + return; // 返回 } } } diff --git a/src/ui(r)/NotesListItem.java b/src/ui(r)/NotesListItem.java index bbd74f5..c34f396 100644 --- a/src/ui(r)/NotesListItem.java +++ b/src/ui(r)/NotesListItem.java @@ -1,131 +1,163 @@ /* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) - * + * 版权声明,表明代码版权归属于The MiCode Open Source Community,时间为2010-2011年。 + */ +/* * Licensed under the Apache License, Version 2.0 (the "License"); + * 声明该文件遵循Apache License 2.0。 * 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 + * http://www.apache.org/licenses/LICENSE-2.0 + * 这是Apache 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; +// 声明该类属于net.micode.notes.ui包。 import android.content.Context; +// 导入Context类,用于获取应用程序的环境信息。 + import android.text.format.DateUtils; +// 导入DateUtils类,用于处理日期和时间。 + import android.view.View; +// 导入View类,用于创建和管理视图。 + import android.widget.CheckBox; +// 导入CheckBox类,用于创建和管理复选框。 + import android.widget.ImageView; +// 导入ImageView类,用于创建和管理图像视图。 + import android.widget.LinearLayout; +// 导入LinearLayout类,用于创建和管理线性布局。 + import android.widget.TextView; +// 导入TextView类,用于创建和管理文本视图。 import net.micode.notes.R; +// 导入R类,用于访问资源文件。 + import net.micode.notes.data.Notes; +// 导入Notes类,用于处理便签数据。 + import net.micode.notes.tool.DataUtils; +// 导入DataUtils类,用于数据处理工具。 + import net.micode.notes.tool.ResourceParser.NoteItemBgResources; +// 导入NoteItemBgResources类,用于获取便签项背景资源。 // NotesListItem是LinearLayout的子类,用于显示单个便签项的视图 public class NotesListItem extends LinearLayout { - private ImageView mAlert; // 用于显示提醒图标 - private TextView mTitle; // 用于显示便签标题 - private TextView mTime; // 用于显示便签修改时间 - private TextView mCallName; // 用于显示通话记录姓名 - private NoteItemData mItemData; // 存储便签项数据 - private CheckBox mCheckBox; // 用于选择模式时显示的复选框 + private ImageView mAlert; // 成员变量,用于显示提醒图标的ImageView + private TextView mTitle; // 成员变量,用于显示便签标题的TextView + private TextView mTime; // 成员变量,用于显示便签修改时间的TextView + private TextView mCallName; // 成员变量,用于显示通话记录姓名的TextView + private NoteItemData mItemData; // 成员变量,存储便签项数据的NoteItemData对象 + private CheckBox mCheckBox; // 成员变量,用于选择模式时显示的复选框CheckBox // 构造函数,初始化视图和组件 public NotesListItem(Context context) { - super(context); - inflate(context, R.layout.note_item, this); // 填充布局 - mAlert = (ImageView) findViewById(R.id.iv_alert_icon); // 初始化提醒图标 - mTitle = (TextView) findViewById(R.id.tv_title); // 初始化标题 - mTime = (TextView) findViewById(R.id.tv_time); // 初始化时间 - mCallName = (TextView) findViewById(R.id.tv_name); // 初始化通话记录姓名 - mCheckBox = (CheckBox) findViewById(android.R.id.checkbox); // 初始化复选框 + super(context); // 调用父类构造函数 + inflate(context, R.layout.note_item, this); // 填充布局,将note_item布局文件加载到当前LinearLayout中 + mAlert = (ImageView) findViewById(R.id.iv_alert_icon); // 通过ID查找提醒图标的ImageView,并赋值给mAlert + mTitle = (TextView) findViewById(R.id.tv_title); // 通过ID查找便签标题的TextView,并赋值给mTitle + mTime = (TextView) findViewById(R.id.tv_time); // 通过ID查找便签修改时间的TextView,并赋值给mTime + mCallName = (TextView) findViewById(R.id.tv_name); // 通过ID查找通话记录姓名的TextView,并赋值给mCallName + mCheckBox = (CheckBox) findViewById(android.R.id.checkbox); // 通过ID查找复选框,并赋值给mCheckBox } // bind方法用于绑定数据和视图 public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) { // 根据选择模式显示或隐藏复选框 if (choiceMode && data.getType() == Notes.TYPE_NOTE) { - mCheckBox.setVisibility(View.VISIBLE); - mCheckBox.setChecked(checked); + mCheckBox.setVisibility(View.VISIBLE); // 如果处于选择模式且类型为便签,则显示复选框 + mCheckBox.setChecked(checked); // 设置复选框的选中状态 } else { - mCheckBox.setVisibility(View.GONE); + mCheckBox.setVisibility(View.GONE); // 否则隐藏复选框 } - mItemData = data; // 保存便签项数据 + mItemData = data; // 保存便签项数据到mItemData + // 根据便签项类型设置视图显示 if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) { - mCallName.setVisibility(View.GONE); - mAlert.setVisibility(View.VISIBLE); - mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); - mTitle.setText(context.getString(R.string.call_record_folder_name) - + context.getString(R.string.format_folder_files_count, data.getNotesCount())); - mAlert.setImageResource(R.drawable.call_record); + mCallName.setVisibility(View.GONE); // 如果是通话记录文件夹,则隐藏通话记录姓名 + mAlert.setVisibility(View.VISIBLE); // 显示提醒图标 + mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); // 设置标题文本样式 + mTitle.setText(context.getString(R.string.call_record_folder_name) // 设置标题文本 + + context.getString(R.string.format_folder_files_count, data.getNotesCount())); // 格式化文件夹文件数量 + mAlert.setImageResource(R.drawable.call_record); // 设置提醒图标资源 } else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) { - mCallName.setVisibility(View.VISIBLE); - mCallName.setText(data.getCallName()); - mTitle.setTextAppearance(context,R.style.TextAppearanceSecondaryItem); - mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); + mCallName.setVisibility(View.VISIBLE); // 如果父ID是通话记录文件夹,则显示通话记录姓名 + mCallName.setText(data.getCallName()); // 设置通话记录姓名文本 + mTitle.setTextAppearance(context, R.style.TextAppearanceSecondaryItem); // 设置标题文本样式 + mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); // 设置标题文本 if (data.hasAlert()) { - mAlert.setImageResource(R.drawable.clock); - mAlert.setVisibility(View.VISIBLE); + mAlert.setImageResource(R.drawable.clock); // 如果有提醒,则设置提醒图标资源 + mAlert.setVisibility(View.VISIBLE); // 显示提醒图标 } else { - mAlert.setVisibility(View.GONE); + mAlert.setVisibility(View.GONE); // 否则隐藏提醒图标 } } else { - mCallName.setVisibility(View.GONE); - mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); + mCallName.setVisibility(View.GONE); // 隐藏通话记录姓名 + mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); // 设置标题文本样式 // 设置标题和提醒图标 if (data.getType() == Notes.TYPE_FOLDER) { - mTitle.setText(data.getSnippet() - + context.getString(R.string.format_folder_files_count, - data.getNotesCount())); - mAlert.setVisibility(View.GONE); + mTitle.setText(data.getSnippet() // 如果类型为文件夹,则设置标题文本 + + context.getString(R.string.format_folder_files_count, data.getNotesCount())); // 格式化文件夹文件数量 + mAlert.setVisibility(View.GONE); // 隐藏提醒图标 } else { - mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); + mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); // 设置标题文本 if (data.hasAlert()) { - mAlert.setImageResource(R.drawable.clock); - mAlert.setVisibility(View.VISIBLE); + mAlert.setImageResource(R.drawable.clock); // 如果有提醒,则设置提醒图标资源 + mAlert.setVisibility(View.VISIBLE); // 显示提醒图标 } else { - mAlert.setVisibility(View.GONE); + mAlert.setVisibility(View.GONE); // 否则隐藏提醒图标 } } } // 设置修改时间显示 - mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate())); + mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate())); // 将修改时间格式化为相对时间字符串 // 设置背景 - setBackground(data); + setBackground(data); // 根据便签项数据设置背景 } // setBackground方法用于设置便签项的背景 private void setBackground(NoteItemData data) { - int id = data.getBgColorId(); + int id = data.getBgColorId(); // 获取便签项的背景色ID if (data.getType() == Notes.TYPE_NOTE) { // 根据便签项的位置和类型设置背景资源 if (data.isSingle() || data.isOneFollowingFolder()) { - setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id)); + setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id)); // 设置单个便签项的背景资源 } else if (data.isLast()) { - setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id)); + setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id)); // 设置最后一个便签项的背景资源 } else if (data.isFirst() || data.isMultiFollowingFolder()) { - setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id)); + setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id)); // 设置第一个便签项的背景资源 } else { - setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id)); + setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id)); // 设置普通便签项的背景资源 } } else { - setBackgroundResource(NoteItemBgResources.getFolderBgRes()); + setBackgroundResource(NoteItemBgResources.getFolderBgRes()); // 如果不是便签项,则设置文件夹的背景资源 } } // getItemData方法用于获取绑定的便签项数据 public NoteItemData getItemData() { - return mItemData; + return mItemData; // 返回绑定的便签项数据 } } \ No newline at end of file