|
|
/*
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
// 便签列表适配器,继承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
|
|
|
mSelectedIndex = new HashMap<Integer, Boolean>(); // 初始化选中项映射
|
|
|
mContext = context; // 保存上下文引用
|
|
|
mNotesCount = 0; // 初始便签数为0
|
|
|
}
|
|
|
|
|
|
// 创建新列表项视图
|
|
|
@Override
|
|
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
|
|
return new NotesListItem(context); // 创建自定义列表项视图
|
|
|
}
|
|
|
|
|
|
// 绑定数据到列表项视图
|
|
|
@Override
|
|
|
public void bindView(View view, Context context, Cursor cursor) {
|
|
|
if (view instanceof NotesListItem) {
|
|
|
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>();
|
|
|
// 遍历选中项映射
|
|
|
for (Integer position : mSelectedIndex.keySet()) {
|
|
|
if (mSelectedIndex.get(position) == true) {
|
|
|
Long id = getItemId(position); // 获取该项的ID
|
|
|
if (id == Notes.ID_ROOT_FOLDER) {
|
|
|
Log.d(TAG, "Wrong item id, should not happen"); // 根文件夹不应被选中
|
|
|
} else {
|
|
|
itemSet.add(id); // 添加到集合
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return itemSet;
|
|
|
}
|
|
|
|
|
|
// 获取选中项关联的小部件属性集合
|
|
|
public HashSet<AppWidgetAttribute> getSelectedWidget() {
|
|
|
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
|
|
|
// 遍历选中项映射
|
|
|
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); // 创建数据对象
|
|
|
widget.widgetId = item.getWidgetId(); // 获取小部件ID
|
|
|
widget.widgetType = item.getWidgetType(); // 获取小部件类型
|
|
|
itemSet.add(widget); // 添加到集合
|
|
|
// 注意:这里不关闭游标,只有适配器可以关闭游标
|
|
|
} else {
|
|
|
Log.e(TAG, "Invalid cursor");
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return itemSet;
|
|
|
}
|
|
|
|
|
|
// 获取选中项数量
|
|
|
public int getSelectedCount() {
|
|
|
Collection<Boolean> values = mSelectedIndex.values();
|
|
|
if (null == values) {
|
|
|
return 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); // 不为0且等于便签总数
|
|
|
}
|
|
|
|
|
|
// 检查指定位置项是否被选中
|
|
|
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;
|
|
|
// 遍历所有项
|
|
|
for (int i = 0; i < getCount(); i++) {
|
|
|
Cursor c = (Cursor) getItem(i);
|
|
|
if (c != null) {
|
|
|
// 只统计便签类型(TYPE_NOTE)
|
|
|
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {
|
|
|
mNotesCount++; // 便签计数加1
|
|
|
}
|
|
|
} else {
|
|
|
Log.e(TAG, "Invalid cursor");
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} |