|
|
/*
|
|
|
* 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;
|
|
|
|
|
|
import android.content.Context;
|
|
|
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";
|
|
|
// 上下文对象,用于获取资源和执行与 UI 相关的操作
|
|
|
private Context mContext;
|
|
|
// 用于存储每个笔记项的选择状态,键为笔记项的位置,值为是否选中的布尔值
|
|
|
private HashMap<Integer, Boolean> mSelectedIndex;
|
|
|
// 笔记的总数
|
|
|
private int mNotesCount;
|
|
|
// 是否处于选择模式的标志
|
|
|
private boolean mChoiceMode;
|
|
|
|
|
|
/**
|
|
|
* 内部类,用于存储小部件的属性,包括小部件 ID 和小部件类型
|
|
|
*/
|
|
|
public static class AppWidgetAttribute {
|
|
|
// 小部件的 ID
|
|
|
public int widgetId;
|
|
|
// 小部件的类型
|
|
|
public int widgetType;
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 构造函数,初始化适配器
|
|
|
* @param context 上下文对象
|
|
|
*/
|
|
|
public NotesListAdapter(Context context) {
|
|
|
// 调用父类的构造函数,传入上下文和初始的 Cursor 为 null
|
|
|
super(context, null);
|
|
|
// 初始化选择状态的 HashMap
|
|
|
mSelectedIndex = new HashMap<Integer, Boolean>();
|
|
|
// 保存上下文对象
|
|
|
mContext = context;
|
|
|
// 初始化笔记总数为 0
|
|
|
mNotesCount = 0;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的视图对象,用于显示笔记项
|
|
|
* @param context 上下文对象
|
|
|
* @param cursor 数据库游标,包含笔记数据
|
|
|
* @param parent 父视图组
|
|
|
* @return 新的 NotesListItem 视图对象
|
|
|
*/
|
|
|
@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) {
|
|
|
// 检查视图是否为 NotesListItem 类型
|
|
|
if (view instanceof NotesListItem) {
|
|
|
// 创建一个 NoteItemData 对象,用于封装笔记数据
|
|
|
NoteItemData itemData = new NoteItemData(context, cursor);
|
|
|
// 调用 NotesListItem 的 bind 方法,将数据绑定到视图上,并传入选择模式和当前项的选择状态
|
|
|
((NotesListItem) view).bind(context, itemData, mChoiceMode,
|
|
|
isSelectedItem(cursor.getPosition()));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置指定位置的笔记项的选择状态
|
|
|
* @param position 笔记项的位置
|
|
|
* @param checked 是否选中
|
|
|
*/
|
|
|
public void setCheckedItem(final int position, final boolean checked) {
|
|
|
// 将指定位置的选择状态保存到 HashMap 中
|
|
|
mSelectedIndex.put(position, checked);
|
|
|
// 通知适配器数据发生变化,刷新视图
|
|
|
notifyDataSetChanged();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 判断适配器是否处于选择模式
|
|
|
* @return 如果处于选择模式返回 true,否则返回 false
|
|
|
*/
|
|
|
public boolean isInChoiceMode() {
|
|
|
return mChoiceMode;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置适配器的选择模式
|
|
|
* @param mode 是否开启选择模式
|
|
|
*/
|
|
|
public void setChoiceMode(boolean mode) {
|
|
|
// 清空选择状态的 HashMap
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取所有选中的笔记项的 ID
|
|
|
* @return 包含所有选中笔记项 ID 的 HashSet
|
|
|
*/
|
|
|
public HashSet<Long> getSelectedItemIds() {
|
|
|
// 创建一个 HashSet 用于存储选中笔记项的 ID
|
|
|
HashSet<Long> itemSet = new HashSet<Long>();
|
|
|
// 遍历选择状态的 HashMap
|
|
|
for (Integer position : mSelectedIndex.keySet()) {
|
|
|
// 检查当前位置的笔记项是否被选中
|
|
|
if (mSelectedIndex.get(position) == true) {
|
|
|
// 获取当前位置的笔记项的 ID
|
|
|
Long id = getItemId(position);
|
|
|
// 检查 ID 是否为根文件夹的 ID,如果是则记录日志,否则将 ID 添加到 HashSet 中
|
|
|
if (id == Notes.ID_ROOT_FOLDER) {
|
|
|
Log.d(TAG, "Wrong item id, should not happen");
|
|
|
} else {
|
|
|
itemSet.add(id);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return itemSet;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取所有选中的小部件的属性
|
|
|
* @return 包含所有选中小部件属性的 HashSet
|
|
|
*/
|
|
|
public HashSet<AppWidgetAttribute> getSelectedWidget() {
|
|
|
// 创建一个 HashSet 用于存储选中小部件的属性
|
|
|
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
|
|
|
// 遍历选择状态的 HashMap
|
|
|
for (Integer position : mSelectedIndex.keySet()) {
|
|
|
// 检查当前位置的笔记项是否被选中
|
|
|
if (mSelectedIndex.get(position) == true) {
|
|
|
// 获取当前位置的数据库游标
|
|
|
Cursor c = (Cursor) getItem(position);
|
|
|
// 检查游标是否有效
|
|
|
if (c != null) {
|
|
|
// 创建一个 AppWidgetAttribute 对象,用于存储小部件的属性
|
|
|
AppWidgetAttribute widget = new AppWidgetAttribute();
|
|
|
// 创建一个 NoteItemData 对象,用于封装笔记数据
|
|
|
NoteItemData item = new NoteItemData(mContext, c);
|
|
|
// 获取小部件的 ID 并保存到 AppWidgetAttribute 对象中
|
|
|
widget.widgetId = item.getWidgetId();
|
|
|
// 获取小部件的类型并保存到 AppWidgetAttribute 对象中
|
|
|
widget.widgetType = item.getWidgetType();
|
|
|
// 将 AppWidgetAttribute 对象添加到 HashSet 中
|
|
|
itemSet.add(widget);
|
|
|
/**
|
|
|
* 不要在这里关闭游标,只有适配器可以关闭它
|
|
|
*/
|
|
|
} else {
|
|
|
// 记录游标无效的错误日志
|
|
|
Log.e(TAG, "Invalid cursor");
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return itemSet;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取选中的笔记项的数量
|
|
|
* @return 选中的笔记项的数量
|
|
|
*/
|
|
|
public int getSelectedCount() {
|
|
|
// 获取选择状态的 HashMap 中的所有值
|
|
|
Collection<Boolean> values = mSelectedIndex.values();
|
|
|
// 检查值集合是否为空
|
|
|
if (null == values) {
|
|
|
return 0;
|
|
|
}
|
|
|
// 创建一个迭代器,用于遍历值集合
|
|
|
Iterator<Boolean> iter = values.iterator();
|
|
|
// 初始化选中笔记项的数量为 0
|
|
|
int count = 0;
|
|
|
// 遍历值集合
|
|
|
while (iter.hasNext()) {
|
|
|
// 检查当前值是否为 true,如果是则增加选中笔记项的数量
|
|
|
if (true == iter.next()) {
|
|
|
count++;
|
|
|
}
|
|
|
}
|
|
|
return count;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 判断是否所有笔记项都被选中
|
|
|
* @return 如果所有笔记项都被选中返回 true,否则返回 false
|
|
|
*/
|
|
|
public boolean isAllSelected() {
|
|
|
// 获取选中的笔记项的数量
|
|
|
int checkedCount = getSelectedCount();
|
|
|
// 检查选中的笔记项数量是否不为 0 且等于笔记总数
|
|
|
return (checkedCount != 0 && checkedCount == mNotesCount);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 判断指定位置的笔记项是否被选中
|
|
|
* @param position 笔记项的位置
|
|
|
* @return 如果指定位置的笔记项被选中返回 true,否则返回 false
|
|
|
*/
|
|
|
public boolean isSelectedItem(final int position) {
|
|
|
// 检查指定位置的选择状态是否为 null,如果是则返回 false,否则返回该位置的选择状态
|
|
|
if (null == mSelectedIndex.get(position)) {
|
|
|
return false;
|
|
|
}
|
|
|
return mSelectedIndex.get(position);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 当内容发生变化时调用,更新笔记总数
|
|
|
*/
|
|
|
@Override
|
|
|
protected void onContentChanged() {
|
|
|
// 调用父类的方法
|
|
|
super.onContentChanged();
|
|
|
// 重新计算笔记总数
|
|
|
calcNotesCount();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 更改适配器使用的数据库游标时调用,更新笔记总数
|
|
|
* @param cursor 新的数据库游标
|
|
|
*/
|
|
|
@Override
|
|
|
public void changeCursor(Cursor cursor) {
|
|
|
// 调用父类的方法
|
|
|
super.changeCursor(cursor);
|
|
|
// 重新计算笔记总数
|
|
|
calcNotesCount();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 计算笔记的总数
|
|
|
*/
|
|
|
private void calcNotesCount() {
|
|
|
// 初始化笔记总数为 0
|
|
|
mNotesCount = 0;
|
|
|
// 遍历所有笔记项
|
|
|
for (int i = 0; i < getCount(); i++) {
|
|
|
// 获取当前位置的数据库游标
|
|
|
Cursor c = (Cursor) getItem(i);
|
|
|
// 检查游标是否有效
|
|
|
if (c != null) {
|
|
|
// 检查当前笔记项的类型是否为普通笔记,如果是则增加笔记总数
|
|
|
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {
|
|
|
mNotesCount++;
|
|
|
}
|
|
|
} else {
|
|
|
// 记录游标无效的错误日志
|
|
|
Log.e(TAG, "Invalid cursor");
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} |