|
|
/*
|
|
|
* 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.
|
|
|
*/
|
|
|
/*功能:在Android应用中显示和管理笔记列表,提供了选中项的管理功能,如全选、全不选、获取选中项的ID集合和小部件属性集合等。
|
|
|
实现方法:通过编写自定义函数CursorAdapter的方法,实现了将数据库中的笔记数据绑定到视图上的功能,并通过一系列辅助方法提供了选中项的管理功能*/
|
|
|
// 导入必要的包和类
|
|
|
package net.micode.notes.ui; // 包名
|
|
|
|
|
|
import android.content.Context; // 上下文,用于与Android系统交互
|
|
|
import android.database.Cursor; // 数据库游标,用于遍历数据库查询结果
|
|
|
import android.util.Log; // 日志工具类,用于输出日志信息
|
|
|
import android.view.View; // 视图基类
|
|
|
import android.view.ViewGroup; // 视图组基类
|
|
|
import android.widget.CursorAdapter; // 适配器基类,用于将数据库查询结果绑定到视图上
|
|
|
|
|
|
import net.micode.notes.data.Notes; // 假设这是包含笔记相关常量和方法的类
|
|
|
|
|
|
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";
|
|
|
|
|
|
// 上下文对象,用于与Android系统交互
|
|
|
private Context mContext;
|
|
|
|
|
|
// 存储选中项的哈希映射,键为位置索引,值为选中状态(true表示选中)
|
|
|
private HashMap<Integer, Boolean> mSelectedIndex;
|
|
|
|
|
|
// 笔记总数
|
|
|
private int mNotesCount;
|
|
|
|
|
|
// 标记是否处于选择模式
|
|
|
private boolean mChoiceMode;
|
|
|
|
|
|
// 定义一个静态内部类,用于存储小部件的属性
|
|
|
public static class AppWidgetAttribute {
|
|
|
public int widgetId; // 小部件ID
|
|
|
public int widgetType; // 小部件类型
|
|
|
}
|
|
|
|
|
|
// 构造函数,初始化成员变量
|
|
|
public NotesListAdapter(Context context) {
|
|
|
super(context, null); // 调用父类构造函数
|
|
|
mSelectedIndex = new HashMap<>(); // 初始化选中项的哈希映射
|
|
|
mContext = context; // 初始化上下文对象
|
|
|
mNotesCount = 0; // 初始化笔记总数为0
|
|
|
}
|
|
|
|
|
|
// 重写newView方法,用于创建新的视图对象
|
|
|
@Override
|
|
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
|
|
return new NotesListItem(context); // 创建一个NotesListItem视图对象
|
|
|
}
|
|
|
|
|
|
// 重写bindView方法,用于绑定视图数据
|
|
|
@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); // 更新选中项的哈希映射
|
|
|
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<Long> getSelectedItemIds() {
|
|
|
HashSet<Long> itemSet = new HashSet<>(); // 创建一个空的哈希集合
|
|
|
for (Integer position : mSelectedIndex.keySet()) { // 遍历选中项的哈希映射的键集
|
|
|
if (mSelectedIndex.get(position) == true) { // 如果选中状态为true
|
|
|
Long id = getItemId(position); // 获取项的ID
|
|
|
if (id == Notes.ID_ROOT_FOLDER) { // 如果是根文件夹ID,则忽略
|
|
|
Log.d(TAG, "Wrong item id, should not happen"); // 输出日志信息
|
|
|
} else {
|
|
|
itemSet.add(id); // 添加ID到集合
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return itemSet; // 返回选中项的ID集合
|
|
|
}
|
|
|
|
|
|
// 获取选中项的小部件属性集合
|
|
|
public HashSet<AppWidgetAttribute> getSelectedWidget() {
|
|
|
HashSet<AppWidgetAttribute> itemSet = new HashSet<>(); // 创建一个空的哈希集合
|
|
|
for (Integer position : mSelectedIndex.keySet()) { // 遍历选中项的哈希映射的键集
|
|
|
if (mSelectedIndex.get(position) == true) { // 如果选中状态为true
|
|
|
Cursor c = (Cursor) getItem(position); // 获取项的游标
|
|
|
if (c != null) { // 如果游标不为空
|
|
|
AppWidgetAttribute widget = new AppWidgetAttribute(); // 创建一个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<Boolean> values = mSelectedIndex.values(); // 获取选中项的哈希映射的值集
|
|
|
if (null == values) {
|
|
|
return 0; // 如果值集为空,则返回0
|
|
|
}
|
|
|
Iterator<Boolean> iter = values.iterator(); // 创建一个迭代器,用于遍历值集
|
|
|
int count = 0; // 初始化计数器为0
|
|
|
while (iter.hasNext()) { // 遍历值集
|
|
|
if (true == iter.next()) { // 如果值为true,表示选中
|
|
|
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)) { // 如果选中项的哈希映射中不存在指定位置的键
|
|
|
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; // 提前返回,结束方法执行
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} |