|
|
|
@ -1,17 +1,14 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
* 版权声明:2010-2011年,MiCode开源社区(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
|
|
|
|
|
* 本代码基于Apache许可证2.0版本发布("许可证");
|
|
|
|
|
* 您可以在遵守许可证的前提下使用本文件;
|
|
|
|
|
* 您可以从以下地址获取许可证副本:
|
|
|
|
|
*
|
|
|
|
|
* 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;
|
|
|
|
@ -30,19 +27,31 @@ import java.util.HashMap;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 便签列表适配器
|
|
|
|
|
* 继承自CursorAdapter,负责将数据库中的便签数据绑定到ListView上
|
|
|
|
|
* 支持多选模式、数据筛选和小部件关联信息获取
|
|
|
|
|
*/
|
|
|
|
|
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>();
|
|
|
|
@ -50,38 +59,65 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
mNotesCount = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建新视图
|
|
|
|
|
* 当ListView需要创建新的便签项视图时调用
|
|
|
|
|
*/
|
|
|
|
|
@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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断是否处于选择模式
|
|
|
|
|
* @return 处于选择模式返回true,否则返回false
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
@ -89,6 +125,10 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取选中项的ID集合
|
|
|
|
|
* @return 包含所有选中项ID的HashSet
|
|
|
|
|
*/
|
|
|
|
|
public HashSet<Long> getSelectedItemIds() {
|
|
|
|
|
HashSet<Long> itemSet = new HashSet<Long>();
|
|
|
|
|
for (Integer position : mSelectedIndex.keySet()) {
|
|
|
|
@ -105,6 +145,10 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
return itemSet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取选中项关联的桌面小部件
|
|
|
|
|
* @return 包含所有选中项关联小部件属性的HashSet
|
|
|
|
|
*/
|
|
|
|
|
public HashSet<AppWidgetAttribute> getSelectedWidget() {
|
|
|
|
|
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
|
|
|
|
|
for (Integer position : mSelectedIndex.keySet()) {
|
|
|
|
@ -117,7 +161,7 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
widget.widgetType = item.getWidgetType();
|
|
|
|
|
itemSet.add(widget);
|
|
|
|
|
/**
|
|
|
|
|
* Don't close cursor here, only the adapter could close it
|
|
|
|
|
* 注意:不要在这里关闭cursor,只有适配器才能关闭它
|
|
|
|
|
*/
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Invalid cursor");
|
|
|
|
@ -128,6 +172,10 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
return itemSet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取选中项数量
|
|
|
|
|
* 统计选中项索引映射表中值为true的项数
|
|
|
|
|
*/
|
|
|
|
|
public int getSelectedCount() {
|
|
|
|
|
Collection<Boolean> values = mSelectedIndex.values();
|
|
|
|
|
if (null == values) {
|
|
|
|
@ -143,11 +191,19 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断是否全选
|
|
|
|
|
* 检查是否所有便签都被选中
|
|
|
|
|
*/
|
|
|
|
|
public boolean isAllSelected() {
|
|
|
|
|
int checkedCount = getSelectedCount();
|
|
|
|
|
return (checkedCount != 0 && checkedCount == mNotesCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断指定位置的项是否被选中
|
|
|
|
|
* @return 选中返回true,否则返回false
|
|
|
|
|
*/
|
|
|
|
|
public boolean isSelectedItem(final int position) {
|
|
|
|
|
if (null == mSelectedIndex.get(position)) {
|
|
|
|
|
return false;
|
|
|
|
@ -155,18 +211,30 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
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++) {
|
|
|
|
|