From 55390d17bed71eba2021e0941f20f3ec382701c2 Mon Sep 17 00:00:00 2001 From: p6zpjanrb <1157949412@qq.com> Date: Fri, 19 Jan 2024 09:32:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/net/micode/notes/ui/NotesListAdapter.java | 45 +++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/net/micode/notes/ui/NotesListAdapter.java b/src/net/micode/notes/ui/NotesListAdapter.java index 51c9cb9..7cbcf34 100644 --- a/src/net/micode/notes/ui/NotesListAdapter.java +++ b/src/net/micode/notes/ui/NotesListAdapter.java @@ -30,19 +30,28 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; - +/* +NotesListAdapter 是一个用于在列表中展示笔记的适配器类,继承自CursorAdapter,用于与数据库中的笔记数据进行交互并在列表中展示。该类中包含了一系列方法用于选择、操作列表项,并在其中还包含了一个内部类AppWidgetAttribute用于存储小部件的属性。 +*/ public class NotesListAdapter extends CursorAdapter { + // 用于日志记录的标签 private static final String TAG = "NotesListAdapter"; + // 应用程序的上下文 private Context mContext; + // 存储选中索引的哈希映射 private HashMap mSelectedIndex; + // 笔记的数量 private int mNotesCount; + // 表示列表是否处于选择模式的标志 private boolean mChoiceMode; + // 内部类,用于存储应用小部件的属性 public static class AppWidgetAttribute { - public int widgetId; - public int widgetType; - }; + public int widgetId; // 小部件的ID + public int widgetType; // 小部件的类型 + } + // 初始化适配器的构造函数 public NotesListAdapter(Context context) { super(context, null); mSelectedIndex = new HashMap(); @@ -50,34 +59,40 @@ public class NotesListAdapter extends CursorAdapter { mNotesCount = 0; } + // 创建一个新的视图来表示列表中的项 @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return new NotesListItem(context); } + // 将视图与指定位置上的数据进行绑定 @Override public void bindView(View view, Context context, Cursor cursor) { if (view instanceof NotesListItem) { + // 从游标中创建NoteItemData,并将其与NotesListItem进行绑定 NoteItemData itemData = new NoteItemData(context, cursor); - ((NotesListItem) view).bind(context, itemData, mChoiceMode, - isSelectedItem(cursor.getPosition())); + ((NotesListItem) view).bind(context, itemData, mChoiceMode, isSelectedItem(cursor.getPosition())); } } + // 设置指定位置上项目的选中状态 public void setCheckedItem(final int position, final boolean checked) { mSelectedIndex.put(position, checked); notifyDataSetChanged(); } + // 检查列表是否处于选择模式 public boolean isInChoiceMode() { return mChoiceMode; } + // 设置列表的选择模式 public void setChoiceMode(boolean mode) { mSelectedIndex.clear(); mChoiceMode = mode; } + // 全选或取消全选列表中的所有项目 public void selectAll(boolean checked) { Cursor cursor = getCursor(); for (int i = 0; i < getCount(); i++) { @@ -89,22 +104,23 @@ public class NotesListAdapter extends CursorAdapter { } } + // 获取选中项目的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"); + Log.d(TAG, "错误的项目ID,不应该发生"); } else { itemSet.add(id); } } } - return itemSet; } + // 获取选中小部件的属性集合 public HashSet getSelectedWidget() { HashSet itemSet = new HashSet(); for (Integer position : mSelectedIndex.keySet()) { @@ -116,11 +132,8 @@ public class NotesListAdapter extends CursorAdapter { widget.widgetId = item.getWidgetId(); widget.widgetType = item.getWidgetType(); itemSet.add(widget); - /** - * Don't close cursor here, only the adapter could close it - */ } else { - Log.e(TAG, "Invalid cursor"); + Log.e(TAG, "无效的游标"); return null; } } @@ -128,6 +141,7 @@ public class NotesListAdapter extends CursorAdapter { return itemSet; } + // 获取选中项目的数量 public int getSelectedCount() { Collection values = mSelectedIndex.values(); if (null == values) { @@ -143,11 +157,13 @@ public class NotesListAdapter extends CursorAdapter { return count; } + // 检查是否全部项目都被选中 public boolean isAllSelected() { int checkedCount = getSelectedCount(); return (checkedCount != 0 && checkedCount == mNotesCount); } + // 检查指定位置上的项目是否被选中 public boolean isSelectedItem(final int position) { if (null == mSelectedIndex.get(position)) { return false; @@ -155,18 +171,21 @@ public class NotesListAdapter extends CursorAdapter { return mSelectedIndex.get(position); } + // 当数据集的内容发生变化时调用 @Override protected void onContentChanged() { super.onContentChanged(); calcNotesCount(); } + // 改变游标 @Override public void changeCursor(Cursor cursor) { super.changeCursor(cursor); calcNotesCount(); } + // 计算列表中笔记的数量 private void calcNotesCount() { mNotesCount = 0; for (int i = 0; i < getCount(); i++) { @@ -176,7 +195,7 @@ public class NotesListAdapter extends CursorAdapter { mNotesCount++; } } else { - Log.e(TAG, "Invalid cursor"); + Log.e(TAG, "无效的游标"); return; } }