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.
TEST1231/NotesListAdapter.java

204 lines
8.6 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;
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";
// 上下文对象,用于访问应用资源
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); // 调用父类构造函数传入上下文和null的Cursor稍后通过swapCursor设置
mSelectedIndex = new HashMap<Integer, Boolean>(); // 初始化选中索引映射
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); // 根据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(); // 获取当前的Cursor
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通常不应被选中
Log.d(TAG, "Wrong item id, should not happen"); // 记录日志
} else {
itemSet.add(id); // 将ID添加到HashSet中
}
}
}
return itemSet; // 返回包含所有选中项ID的HashSet
}
} // 获取已选择的小部件集合
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); // 将小部件属性添加到HashSet中
/**
* 不要在这里关闭Cursor只有适配器可以关闭它
*/
} else {
Log.e(TAG, "Invalid cursor"); // 如果Cursor为空记录错误日志
return null; // 返回null表示出错
}
}
}
return itemSet; // 返回包含所有已选择小部件属性的HashSet
}
// 获取已选择项的数量
public int getSelectedCount() {
Collection<Boolean> values = mSelectedIndex.values(); // 获取所有索引值的集合
if (null == values) {
return 0; // 如果集合为空返回0
}
Iterator<Boolean> iter = values.iterator(); // 创建迭代器遍历集合
int count = 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; // 如果指定位置的索引值为null返回false
}
return mSelectedIndex.get(position); // 返回指定位置的索引值
}
// 当内容发生变化时调用
@Override
protected void onContentChanged() {
super.onContentChanged(); // 调用父类方法
calcNotesCount(); // 计算笔记数量
}
// 当Cursor改变时调用
@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); // 获取当前项的Cursor
if (c != null) { // 如果Cursor不为空
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) { // 如果Cursor指向的笔记类型是NOTE
mNotesCount++; // 笔记数量加1
}
} else {
Log.e(TAG, "Invalid cursor"); // 如果Cursor为空记录错误日志
return; // 退出方法
}
}
}