parent
a7af2c8d80
commit
14a8d917d1
@ -1,184 +1,167 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
|
||||
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;
|
||||
private Context mContext; // 上下文对象
|
||||
private HashMap<Integer, Boolean> mSelectedIndex; // 存储选中位置的映射表(位置 -> 选中状态)
|
||||
private int mNotesCount; // 普通笔记总数(排除文件夹)
|
||||
private boolean mChoiceMode; // 是否处于多选模式
|
||||
|
||||
// 小部件属性封装类
|
||||
public static class AppWidgetAttribute {
|
||||
public int widgetId;
|
||||
public int widgetType;
|
||||
};
|
||||
public int widgetId; // 小部件ID
|
||||
public int widgetType; // 小部件类型
|
||||
}
|
||||
|
||||
// 构造函数
|
||||
public NotesListAdapter(Context context) {
|
||||
super(context, null);
|
||||
mSelectedIndex = new HashMap<Integer, Boolean>();
|
||||
mContext = context;
|
||||
mNotesCount = 0;
|
||||
super(context, null); // 初始化CursorAdapter
|
||||
mSelectedIndex = new HashMap<Integer, Boolean>(); // 初始化选中状态容器
|
||||
mContext = context; // 保存上下文引用
|
||||
mNotesCount = 0; // 初始化笔记数量
|
||||
}
|
||||
|
||||
// 创建新列表项视图
|
||||
@Override
|
||||
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
||||
return new NotesListItem(context);
|
||||
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()));
|
||||
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();
|
||||
mSelectedIndex.put(position, checked); // 更新选中状态映射表
|
||||
notifyDataSetChanged(); // 触发视图刷新
|
||||
}
|
||||
|
||||
// 检查是否处于多选模式
|
||||
public boolean isInChoiceMode() {
|
||||
return mChoiceMode;
|
||||
return mChoiceMode; // 返回当前多选模式状态
|
||||
}
|
||||
|
||||
// 切换多选模式
|
||||
public void setChoiceMode(boolean mode) {
|
||||
mSelectedIndex.clear();
|
||||
mChoiceMode = 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);
|
||||
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);
|
||||
if (id == Notes.ID_ROOT_FOLDER) {
|
||||
HashSet<Long> itemSet = new HashSet<Long>(); // 创建ID集合
|
||||
for (Integer position : mSelectedIndex.keySet()) { // 遍历选中项
|
||||
if (mSelectedIndex.get(position)) { // 检查选中状态
|
||||
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);
|
||||
itemSet.add(id); // 添加有效ID到集合
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return itemSet;
|
||||
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);
|
||||
for (Integer position : mSelectedIndex.keySet()) { // 遍历选中位置
|
||||
if (mSelectedIndex.get(position)) { // 检查选中状态
|
||||
Cursor c = (Cursor) getItem(position); // 获取对应游标
|
||||
if (c != null) {
|
||||
AppWidgetAttribute widget = new AppWidgetAttribute();
|
||||
NoteItemData item = new NoteItemData(mContext, c);
|
||||
widget.widgetId = item.getWidgetId();
|
||||
widget.widgetType = item.getWidgetType();
|
||||
itemSet.add(widget);
|
||||
/**
|
||||
* Don't close cursor here, only the adapter could close it
|
||||
*/
|
||||
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;
|
||||
Log.e(TAG, "Invalid cursor"); // 错误日志记录
|
||||
return null; // 返回空值
|
||||
}
|
||||
}
|
||||
}
|
||||
return itemSet;
|
||||
return itemSet; // 返回结果集合
|
||||
}
|
||||
|
||||
// 统计选中项数量
|
||||
public int getSelectedCount() {
|
||||
Collection<Boolean> values = mSelectedIndex.values();
|
||||
if (null == values) {
|
||||
return 0;
|
||||
}
|
||||
Iterator<Boolean> iter = values.iterator();
|
||||
int count = 0;
|
||||
while (iter.hasNext()) {
|
||||
if (true == iter.next()) {
|
||||
count++;
|
||||
}
|
||||
Collection<Boolean> values = mSelectedIndex.values(); // 获取所有状态值
|
||||
if (values == null) return 0; // 空值保护
|
||||
Iterator<Boolean> iter = values.iterator(); // 创建迭代器
|
||||
int count = 0; // 计数器初始化
|
||||
while (iter.hasNext()) { // 遍历状态集合
|
||||
if (iter.next()) count++; // 统计选中状态数量
|
||||
}
|
||||
return count;
|
||||
return count; // 返回总数
|
||||
}
|
||||
|
||||
// 检查是否全选
|
||||
public boolean isAllSelected() {
|
||||
int checkedCount = getSelectedCount();
|
||||
return (checkedCount != 0 && checkedCount == mNotesCount);
|
||||
int checkedCount = getSelectedCount(); // 获取当前选中数
|
||||
return (checkedCount != 0 && checkedCount == mNotesCount); // 比较总数
|
||||
}
|
||||
|
||||
// 检查指定位置是否选中
|
||||
public boolean isSelectedItem(final int position) {
|
||||
if (null == mSelectedIndex.get(position)) {
|
||||
return false;
|
||||
}
|
||||
return mSelectedIndex.get(position);
|
||||
Boolean result = mSelectedIndex.get(position); // 获取选中状态
|
||||
return result != null ? result : false; // 空值安全处理
|
||||
}
|
||||
|
||||
// 数据变更回调
|
||||
@Override
|
||||
protected void onContentChanged() {
|
||||
super.onContentChanged();
|
||||
calcNotesCount();
|
||||
super.onContentChanged(); // 调用父类方法
|
||||
calcNotesCount(); // 重新计算笔记数量
|
||||
}
|
||||
|
||||
// 切换数据游标
|
||||
@Override
|
||||
public void changeCursor(Cursor cursor) {
|
||||
super.changeCursor(cursor);
|
||||
calcNotesCount();
|
||||
super.changeCursor(cursor); // 调用父类方法
|
||||
calcNotesCount(); // 重新计算笔记数量
|
||||
}
|
||||
|
||||
// 计算普通笔记数量
|
||||
private void calcNotesCount() {
|
||||
mNotesCount = 0;
|
||||
for (int i = 0; i < getCount(); i++) {
|
||||
Cursor c = (Cursor) getItem(i);
|
||||
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++;
|
||||
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) { // 类型检查
|
||||
mNotesCount++; // 计数普通笔记
|
||||
}
|
||||
} else {
|
||||
Log.e(TAG, "Invalid cursor");
|
||||
return;
|
||||
Log.e(TAG, "Invalid cursor"); // 错误日志
|
||||
return; // 提前退出
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in new issue