|
|
@ -30,19 +30,28 @@ import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
NotesListAdapter 是一个用于在列表中展示笔记的适配器类,继承自CursorAdapter,用于与数据库中的笔记数据进行交互并在列表中展示。该类中包含了一系列方法用于选择、操作列表项,并在其中还包含了一个内部类AppWidgetAttribute用于存储小部件的属性。
|
|
|
|
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
mSelectedIndex = new HashMap<Integer, Boolean>();
|
|
|
|
mSelectedIndex = new HashMap<Integer, Boolean>();
|
|
|
@ -50,34 +59,40 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
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,并将其与NotesListItem进行绑定
|
|
|
|
NoteItemData itemData = new NoteItemData(context, cursor);
|
|
|
|
NoteItemData itemData = new NoteItemData(context, cursor);
|
|
|
|
((NotesListItem) view).bind(context, itemData, mChoiceMode,
|
|
|
|
((NotesListItem) view).bind(context, itemData, mChoiceMode, isSelectedItem(cursor.getPosition()));
|
|
|
|
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++) {
|
|
|
@ -89,22 +104,23 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取选中项目的ID集合
|
|
|
|
public HashSet<Long> getSelectedItemIds() {
|
|
|
|
public HashSet<Long> getSelectedItemIds() {
|
|
|
|
HashSet<Long> itemSet = new HashSet<Long>();
|
|
|
|
HashSet<Long> itemSet = new HashSet<Long>();
|
|
|
|
for (Integer position : mSelectedIndex.keySet()) {
|
|
|
|
for (Integer position : mSelectedIndex.keySet()) {
|
|
|
|
if (mSelectedIndex.get(position) == true) {
|
|
|
|
if (mSelectedIndex.get(position) == true) {
|
|
|
|
Long id = getItemId(position);
|
|
|
|
Long id = getItemId(position);
|
|
|
|
if (id == Notes.ID_ROOT_FOLDER) {
|
|
|
|
if (id == Notes.ID_ROOT_FOLDER) {
|
|
|
|
Log.d(TAG, "Wrong item id, should not happen");
|
|
|
|
Log.d(TAG, "错误的项目ID,不应该发生");
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
itemSet.add(id);
|
|
|
|
itemSet.add(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()) {
|
|
|
@ -116,11 +132,8 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
widget.widgetId = item.getWidgetId();
|
|
|
|
widget.widgetId = item.getWidgetId();
|
|
|
|
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, "无效的游标");
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -128,6 +141,7 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
return itemSet;
|
|
|
|
return itemSet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取选中项目的数量
|
|
|
|
public int getSelectedCount() {
|
|
|
|
public int getSelectedCount() {
|
|
|
|
Collection<Boolean> values = mSelectedIndex.values();
|
|
|
|
Collection<Boolean> values = mSelectedIndex.values();
|
|
|
|
if (null == values) {
|
|
|
|
if (null == values) {
|
|
|
@ -143,11 +157,13 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
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)) {
|
|
|
|
if (null == mSelectedIndex.get(position)) {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
@ -155,18 +171,21 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
return mSelectedIndex.get(position);
|
|
|
|
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++) {
|
|
|
@ -176,7 +195,7 @@ public class NotesListAdapter extends CursorAdapter {
|
|
|
|
mNotesCount++;
|
|
|
|
mNotesCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Log.e(TAG, "Invalid cursor");
|
|
|
|
Log.e(TAG, "无效的游标");
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|