/* * 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; // 导入所需的Android SDK类和包 import android.content.Context; import android.database.Cursor; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.CursorAdapter; // 导入Notes应用特有的数据类 import net.micode.notes.data.Notes; // 导入Java的集合框架 import java.util.Collection; import java.util.HashMap; 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; // 是否处于选择模式 // AppWidgetAttribute内部类,用于存储应用小部件的属性 public static class AppWidgetAttribute { public int widgetId; // 小部件ID public int widgetType; // 小部件类型 }; // 构造函数,初始化适配器 public NotesListAdapter(Context context) { super(context, null); // 调用父类的构造函数 mSelectedIndex = new HashMap(); // 初始化选中状态的HashMap mContext = context; // 保存上下文对象 mNotesCount = 0; // 初始化笔记数量为0 } // 新建视图的方法,用于创建新的列表项视图 @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return new NotesListItem(context); // 返回一个新的NotesListItem视图 } // 绑定视图的方法,用于将数据绑定到视图上 @Override public void bindView(View view, Context context, Cursor cursor) { if (view instanceof NotesListItem) { // 检查视图是否是NotesListItem的实例 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); // 在HashMap中设置选中状态 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++) { // 遍历所有项 if (cursor.moveToPosition(i)) { // 移动游标到当前位置 if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { // 如果是笔记类型 setCheckedItem(i, checked); // 设置选中状态 } } } } // 获取所有选中的笔记ID public HashSet getSelectedItemIds() { 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 itemSet.add(id); // 添加到集合中 } } } return itemSet; // 返回集合 } // 获取所有选中的应用小部件属性 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); // 创建NoteItemData对象 widget.widgetId = item.getWidgetId(); // 获取小部件ID widget.widgetType = item.getWidgetType(); // 获取小部件类型 itemSet.add(widget); // 添加到集合中 } else { Log.e(TAG, "Invalid cursor"); // 日志错误 return null; // 返回null } } } return itemSet; // 返回集合 } // 获取选中的数量 public int getSelectedCount() { Collection values = mSelectedIndex.values(); // 获取所有选中状态的值 if (null == values) { return 0; // 如果为空,返回0 } Iterator iter = values.iterator(); // 创建迭代器 int count = 0; // 初始化计数器 while (iter.hasNext()) { // 遍历所有值 if (true == iter.next()) { // 如果是选中的 count++; // 计数器加1 } } return count; // 返回计数 } // 检查是否全部选中 public boolean isAllSelected() { int checkedCount = getSelectedCount(); // 获取选中的数量 return (checkedCount != 0 && checkedCount == mNotesCount); // 如果选中数量不为0且等于笔记数量,则返回true } // 检查某个位置的项是否被选中 public boolean isSelectedItem(final int position) { if (null == mSelectedIndex.get(position)) { // 如果HashMap中没有这个位置的值 return false; // 返回false } 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; // 初始化笔记数量为0 for (int i = 0; i < getCount(); i++) { // 遍历所有项 Cursor c = (Cursor) getItem(i); // 获取游标 if (c != null) { // 如果游标不为空 if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) { // 如果是笔记类型 mNotesCount++; // 笔记数量加1 } } else { Log.e(TAG, "Invalid cursor"); // 日志错误 return; // 返回 } } } }