You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
xiaomi-Notes/NotesListAdapter.java

215 lines
8.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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相关的库用于处理数据库查询和视图的适配
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; // 用于遍历集合元素
// 自定义适配器,用于显示笔记列表
public class NotesListAdapter extends CursorAdapter {
private static final String TAG = "NotesListAdapter"; // 日志标签
private Context mContext; // 上下文对象,用于访问应用资源
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<Integer, Boolean>(); // 初始化存储选中项的 HashMap
mContext = context; // 设置上下文
mNotesCount = 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 对象
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(); // 通知数据发生变化,刷新视图
}
// 判断是否处于选择模式
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<Long>(); // 创建一个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记录日志并跳过该项
Log.d(TAG, "Wrong item id, should not happen");
} else {
itemSet.add(id); // 将ID添加到itemSet中
}
}
}
return itemSet; // 返回所有选中项的ID集合
}
// 获取已选择的小部件AppWidget属性集合
public HashSet<AppWidgetAttribute> getSelectedWidget() {
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>(); // 创建一个HashSet来存储选中的小部件属性
// 遍历所有选中的项的索引
for (Integer position : mSelectedIndex.keySet()) {
if (mSelectedIndex.get(position) == true) { // 判断该项是否被选中
Cursor c = (Cursor) getItem(position); // 获取该项的Cursor对象
if (c != null) { // 如果Cursor不为空则处理该项
AppWidgetAttribute widget = new AppWidgetAttribute(); // 创建一个AppWidgetAttribute对象
NoteItemData item = new NoteItemData(mContext, c); // 使用Cursor对象初始化NoteItemData
widget.widgetId = item.getWidgetId(); // 获取小部件ID
widget.widgetType = item.getWidgetType(); // 获取小部件类型
itemSet.add(widget); // 将小部件属性添加到itemSet中
// 注意不要在这里关闭Cursor由适配器负责关闭
} else {
Log.e(TAG, "Invalid cursor"); // 如果Cursor为空记录错误日志并返回null
return 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;
// 遍历选中项状态如果为true则计数
while (iter.hasNext()) {
if (true == iter.next()) {
count++;
}
}
return count; // 返回选中项的数量
}
// 判断是否所有项都被选中
public boolean isAllSelected() {
int checkedCount = getSelectedCount(); // 获取已选中项的数量
return (checkedCount != 0 && checkedCount == mNotesCount); // 如果已选中项的数量等于总项数则返回true
}
// 判断某个位置的项是否被选中
public boolean isSelectedItem(final int position) {
if (null == mSelectedIndex.get(position)) {
return false; // 如果该位置没有被选中返回false
}
return mSelectedIndex.get(position); // 返回该位置的选中状态
@Override
protected void onContentChanged() {
// 当内容发生变化时调用通常用于刷新UI或更新数据
super.onContentChanged();
// 重新计算笔记的数量
calcNotesCount();
}
@Override
public void changeCursor(Cursor cursor) {
// 当游标Cursor发生变化时调用通常在数据更新时触发
super.changeCursor(cursor);
// 重新计算笔记的数量
calcNotesCount();
}
private void calcNotesCount() {
// 初始化笔记数量为0
mNotesCount = 0;
// 遍历当前的数据项假设这些数据项是通过Cursor获取的
for (int i = 0; i < getCount(); i++) {
// 获取当前位置的Cursor对象
Cursor c = (Cursor) getItem(i);
if (c != null) {
// 如果当前游标存在,检查数据项类型是否为笔记类型
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {
// 如果是笔记类型,增加笔记数量
mNotesCount++;
}
} else {
// 如果游标为null打印错误日志并返回
Log.e(TAG, "Invalid cursor");
return;
}
}
}
}