diff --git a/src/Notes-master/src/net/micode/notes/ui/NotesListAdapter.java b/src/Notes-master/src/net/micode/notes/ui/NotesListAdapter.java index 51c9cb9..45e6147 100644 --- a/src/Notes-master/src/net/micode/notes/ui/NotesListAdapter.java +++ b/src/Notes-master/src/net/micode/notes/ui/NotesListAdapter.java @@ -31,18 +31,29 @@ import java.util.HashSet; import java.util.Iterator; +/** + * NotesListAdapter是用于展示笔记列表的适配器,继承自CursorAdapter + * 支持多选模式,可对笔记进行选择操作 + */ public class NotesListAdapter extends CursorAdapter { private static final String TAG = "NotesListAdapter"; - private Context mContext; - private HashMap mSelectedIndex; - private int mNotesCount; - private boolean mChoiceMode; + private Context mContext; // 上下文环境 + private HashMap mSelectedIndex; // 存储选中项的索引和状态 + private int mNotesCount; // 笔记总数 + private boolean mChoiceMode; // 是否处于选择模式 + /** + * 应用小部件属性类,用于存储小部件ID和类型 + */ public static class AppWidgetAttribute { - public int widgetId; - public int widgetType; + public int widgetId; // 小部件ID + public int widgetType; // 小部件类型 }; + /** + * 构造函数,初始化适配器 + * @param context 上下文环境 + */ public NotesListAdapter(Context context) { super(context, null); mSelectedIndex = new HashMap(); @@ -50,38 +61,71 @@ public class NotesListAdapter extends CursorAdapter { mNotesCount = 0; } + /** + * 创建新的视图 + * @param context 上下文环境 + * @param cursor 数据集游标 + * @param parent 父视图组 + * @return 返回新的笔记列表项视图 + */ @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return new NotesListItem(context); } + /** + * 绑定数据到视图 + * @param view 目标视图 + * @param context 上下文环境 + * @param cursor 数据集游标 + */ @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())); } } + /** + * 设置指定位置项的选中状态 + * @param position 位置索引 + * @param checked 选中状态 + */ public void setCheckedItem(final int position, final boolean checked) { mSelectedIndex.put(position, checked); - notifyDataSetChanged(); + notifyDataSetChanged(); // 通知数据变化,刷新视图 } + /** + * 判断是否处于选择模式 + * @return 是/否 + */ public boolean isInChoiceMode() { return mChoiceMode; } + /** + * 设置选择模式 + * @param mode 选择模式 + */ public void setChoiceMode(boolean mode) { - mSelectedIndex.clear(); - mChoiceMode = mode; + mSelectedIndex.clear(); // 清除已选项目 + mChoiceMode = mode; // 设置选择模式 } + /** + * 全选或取消全选操作 + * @param checked 全选/取消全选 + */ public void selectAll(boolean checked) { Cursor cursor = getCursor(); for (int i = 0; i < getCount(); i++) { if (cursor.moveToPosition(i)) { + // 只对笔记类型进行操作,文件夹不处理 if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { setCheckedItem(i, checked); } @@ -89,6 +133,10 @@ public class NotesListAdapter extends CursorAdapter { } } + /** + * 获取选中项的ID集合 + * @return 选中项ID的集合 + */ public HashSet getSelectedItemIds() { HashSet itemSet = new HashSet(); for (Integer position : mSelectedIndex.keySet()) { @@ -101,10 +149,13 @@ public class NotesListAdapter extends CursorAdapter { } } } - return itemSet; } + /** + * 获取选中的小部件集合 + * @return 选中的小部件属性集合 + */ public HashSet getSelectedWidget() { HashSet itemSet = new HashSet(); for (Integer position : mSelectedIndex.keySet()) { @@ -128,6 +179,10 @@ public class NotesListAdapter extends CursorAdapter { return itemSet; } + /** + * 获取选中项的数量 + * @return 选中项数量 + */ public int getSelectedCount() { Collection values = mSelectedIndex.values(); if (null == values) { @@ -143,11 +198,20 @@ public class NotesListAdapter extends CursorAdapter { return count; } + /** + * 判断是否已全选 + * @return 是/否 + */ public boolean isAllSelected() { int checkedCount = getSelectedCount(); return (checkedCount != 0 && checkedCount == mNotesCount); } + /** + * 判断指定位置的项是否被选中 + * @param position 位置索引 + * @return 是/否 + */ public boolean isSelectedItem(final int position) { if (null == mSelectedIndex.get(position)) { return false; @@ -155,18 +219,28 @@ public class NotesListAdapter extends CursorAdapter { return mSelectedIndex.get(position); } + /** + * 当内容发生变化时调用 + */ @Override protected void onContentChanged() { super.onContentChanged(); - calcNotesCount(); + calcNotesCount(); // 重新计算笔记数量 } + /** + * 更改游标时调用 + * @param cursor 新的游标 + */ @Override public void changeCursor(Cursor cursor) { super.changeCursor(cursor); - calcNotesCount(); + calcNotesCount(); // 重新计算笔记数量 } + /** + * 计算笔记数量,只统计类型为笔记的项 + */ private void calcNotesCount() { mNotesCount = 0; for (int i = 0; i < getCount(); i++) { @@ -181,4 +255,4 @@ public class NotesListAdapter extends CursorAdapter { } } } -} +} \ No newline at end of file