From c9ceaa5d7c81fed3c6ba6408b9bb2d9c69563a7a Mon Sep 17 00:00:00 2001 From: MRD <1259303886@qq.com> Date: Fri, 13 Jun 2025 17:36:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=A3=E7=A0=81=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/micode/notes/ui/NotesListAdapter.java | 106 ++++++++++++++---- 1 file changed, 87 insertions(+), 19 deletions(-) diff --git a/src/Notes-master/app/src/main/java/net/micode/notes/ui/NotesListAdapter.java b/src/Notes-master/app/src/main/java/net/micode/notes/ui/NotesListAdapter.java index 51c9cb9..4a6e179 100644 --- a/src/Notes-master/app/src/main/java/net/micode/notes/ui/NotesListAdapter.java +++ b/src/Notes-master/app/src/main/java/net/micode/notes/ui/NotesListAdapter.java @@ -1,17 +1,14 @@ /* - * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * 版权声明:2010-2011年,MiCode开源社区(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 + * 本代码基于Apache许可证2.0版本发布("许可证"); + * 您可以在遵守许可证的前提下使用本文件; + * 您可以从以下地址获取许可证副本: * * 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; @@ -30,19 +27,31 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; - +/** + * 便签列表适配器 + * 继承自CursorAdapter,负责将数据库中的便签数据绑定到ListView上 + * 支持多选模式、数据筛选和小部件关联信息获取 + */ 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; // 是否处于选择模式 + + /** + * 桌面小部件属性类 + * 用于存储便签关联的桌面小部件信息 + */ 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,38 +59,65 @@ public class NotesListAdapter extends CursorAdapter { mNotesCount = 0; } + /** + * 创建新视图 + * 当ListView需要创建新的便签项视图时调用 + */ @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 itemData = new NoteItemData(context, cursor); + // 绑定数据到便签项视图,设置选择状态 ((NotesListItem) view).bind(context, itemData, mChoiceMode, isSelectedItem(cursor.getPosition())); } } + /** + * 设置项的选中状态 + * 更新选中项索引映射表并通知适配器数据已改变 + */ public void setCheckedItem(final int position, final boolean checked) { mSelectedIndex.put(position, checked); notifyDataSetChanged(); } + /** + * 判断是否处于选择模式 + * @return 处于选择模式返回true,否则返回false + */ 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++) { if (cursor.moveToPosition(i)) { + // 只处理类型为便签的项 if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { setCheckedItem(i, checked); } @@ -89,6 +125,10 @@ public class NotesListAdapter extends CursorAdapter { } } + /** + * 获取选中项的ID集合 + * @return 包含所有选中项ID的HashSet + */ public HashSet getSelectedItemIds() { HashSet itemSet = new HashSet(); for (Integer position : mSelectedIndex.keySet()) { @@ -105,6 +145,10 @@ public class NotesListAdapter extends CursorAdapter { return itemSet; } + /** + * 获取选中项关联的桌面小部件 + * @return 包含所有选中项关联小部件属性的HashSet + */ public HashSet getSelectedWidget() { HashSet itemSet = new HashSet(); for (Integer position : mSelectedIndex.keySet()) { @@ -117,7 +161,7 @@ public class NotesListAdapter extends CursorAdapter { widget.widgetType = item.getWidgetType(); itemSet.add(widget); /** - * Don't close cursor here, only the adapter could close it + * 注意:不要在这里关闭cursor,只有适配器才能关闭它 */ } else { Log.e(TAG, "Invalid cursor"); @@ -128,6 +172,10 @@ public class NotesListAdapter extends CursorAdapter { return itemSet; } + /** + * 获取选中项数量 + * 统计选中项索引映射表中值为true的项数 + */ public int getSelectedCount() { Collection values = mSelectedIndex.values(); if (null == values) { @@ -143,11 +191,19 @@ public class NotesListAdapter extends CursorAdapter { return count; } + /** + * 判断是否全选 + * 检查是否所有便签都被选中 + */ public boolean isAllSelected() { int checkedCount = getSelectedCount(); return (checkedCount != 0 && checkedCount == mNotesCount); } + /** + * 判断指定位置的项是否被选中 + * @return 选中返回true,否则返回false + */ public boolean isSelectedItem(final int position) { if (null == mSelectedIndex.get(position)) { return false; @@ -155,18 +211,30 @@ 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++) { @@ -181,4 +249,4 @@ public class NotesListAdapter extends CursorAdapter { } } } -} +} \ No newline at end of file