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