|
|
|
@ -30,19 +30,27 @@ 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;
|
|
|
|
|
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;
|
|
|
|
|
public int widgetType;
|
|
|
|
|
};
|
|
|
|
|
public int widgetId; // 小部件ID
|
|
|
|
|
public int widgetType; // 小部件类型
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数
|
|
|
|
|
* @param context 上下文对象
|
|
|
|
|
*/
|
|
|
|
|
public NotesListAdapter(Context context) {
|
|
|
|
|
super(context, null);
|
|
|
|
|
mSelectedIndex = new HashMap<Integer, Boolean>();
|
|
|
|
@ -50,36 +58,68 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
mNotesCount = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建新视图
|
|
|
|
|
* @param context 上下文对象
|
|
|
|
|
* @param cursor 数据游标
|
|
|
|
|
* @param parent 父视图组
|
|
|
|
|
* @return 返回新建的NotesListItem视图
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
|
|
|
|
return new NotesListItem(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 绑定数据到视图
|
|
|
|
|
* @param view 要绑定的视图
|
|
|
|
|
* @param context 上下文对象
|
|
|
|
|
* @param cursor 数据游标
|
|
|
|
|
*/
|
|
|
|
|
@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()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置项选中状态
|
|
|
|
|
* @param position 项位置
|
|
|
|
|
* @param checked 是否选中
|
|
|
|
|
*/
|
|
|
|
|
public void setCheckedItem(final int position, final boolean checked) {
|
|
|
|
|
mSelectedIndex.put(position, checked);
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
|
notifyDataSetChanged(); // 通知数据变化
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查是否处于多选模式
|
|
|
|
|
* @return 是否处于多选模式
|
|
|
|
|
*/
|
|
|
|
|
public boolean isInChoiceMode() {
|
|
|
|
|
return mChoiceMode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置多选模式
|
|
|
|
|
* @param mode 是否启用多选模式
|
|
|
|
|
*/
|
|
|
|
|
public void setChoiceMode(boolean mode) {
|
|
|
|
|
mSelectedIndex.clear();
|
|
|
|
|
mSelectedIndex.clear(); // 清空选中状态
|
|
|
|
|
mChoiceMode = mode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 全选/取消全选
|
|
|
|
|
* @param checked 是否选中所有项
|
|
|
|
|
*/
|
|
|
|
|
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) {
|
|
|
|
@ -89,11 +129,16 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取选中项的ID集合
|
|
|
|
|
* @return 选中项的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 {
|
|
|
|
@ -101,10 +146,13 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return itemSet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取选中小部件属性集合
|
|
|
|
|
* @return 小部件属性集合
|
|
|
|
|
*/
|
|
|
|
|
public HashSet<AppWidgetAttribute> getSelectedWidget() {
|
|
|
|
|
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
|
|
|
|
|
for (Integer position : mSelectedIndex.keySet()) {
|
|
|
|
@ -116,9 +164,7 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
widget.widgetId = item.getWidgetId();
|
|
|
|
|
widget.widgetType = item.getWidgetType();
|
|
|
|
|
itemSet.add(widget);
|
|
|
|
|
/**
|
|
|
|
|
* Don't close cursor here, only the adapter could close it
|
|
|
|
|
*/
|
|
|
|
|
// 注意:不要在此关闭游标,只有适配器可以关闭它
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Invalid cursor");
|
|
|
|
|
return null;
|
|
|
|
@ -128,6 +174,10 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
return itemSet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取选中项数量
|
|
|
|
|
* @return 选中项数量
|
|
|
|
|
*/
|
|
|
|
|
public int getSelectedCount() {
|
|
|
|
|
Collection<Boolean> values = mSelectedIndex.values();
|
|
|
|
|
if (null == values) {
|
|
|
|
@ -143,11 +193,20 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查是否全部选中
|
|
|
|
|
* @return 是否全部选中
|
|
|
|
|
*/
|
|
|
|
|
public boolean isAllSelected() {
|
|
|
|
|
int checkedCount = getSelectedCount();
|
|
|
|
|
return (checkedCount != 0 && checkedCount == mNotesCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查指定位置项是否被选中
|
|
|
|
|
* @param position 项位置
|
|
|
|
|
* @return 是否被选中
|
|
|
|
|
*/
|
|
|
|
|
public boolean isSelectedItem(final int position) {
|
|
|
|
|
if (null == mSelectedIndex.get(position)) {
|
|
|
|
|
return false;
|
|
|
|
@ -155,18 +214,28 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
return mSelectedIndex.get(position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 内容变化回调
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected void onContentChanged() {
|
|
|
|
|
super.onContentChanged();
|
|
|
|
|
calcNotesCount();
|
|
|
|
|
calcNotesCount(); // 重新计算笔记数量
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更换游标
|
|
|
|
|
* @param cursor 新游标
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void changeCursor(Cursor cursor) {
|
|
|
|
|
super.changeCursor(cursor);
|
|
|
|
|
calcNotesCount();
|
|
|
|
|
calcNotesCount(); // 重新计算笔记数量
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 计算笔记数量(不包括文件夹)
|
|
|
|
|
*/
|
|
|
|
|
private void calcNotesCount() {
|
|
|
|
|
mNotesCount = 0;
|
|
|
|
|
for (int i = 0; i < getCount(); i++) {
|
|
|
|
@ -181,4 +250,4 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|