代码注释

main
p6zpjanrb 1 year ago
parent eb8b68abad
commit 55390d17be

@ -30,19 +30,28 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
/*
NotesListAdapter CursorAdapterAppWidgetAttribute
*/
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;
// 内部类,用于存储应用小部件的属性
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,34 +59,40 @@ public class NotesListAdapter extends CursorAdapter {
mNotesCount = 0;
}
// 创建一个新的视图来表示列表中的项
@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并将其与NotesListItem进行绑定
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) {
mSelectedIndex.put(position, checked);
notifyDataSetChanged();
}
// 检查列表是否处于选择模式
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++) {
@ -89,22 +104,23 @@ public class NotesListAdapter extends CursorAdapter {
}
}
// 获取选中项目的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) {
Log.d(TAG, "Wrong item id, should not happen");
Log.d(TAG, "错误的项目ID不应该发生");
} else {
itemSet.add(id);
}
}
}
return itemSet;
}
// 获取选中小部件的属性集合
public HashSet<AppWidgetAttribute> getSelectedWidget() {
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
for (Integer position : mSelectedIndex.keySet()) {
@ -116,11 +132,8 @@ 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");
Log.e(TAG, "无效的游标");
return null;
}
}
@ -128,6 +141,7 @@ public class NotesListAdapter extends CursorAdapter {
return itemSet;
}
// 获取选中项目的数量
public int getSelectedCount() {
Collection<Boolean> values = mSelectedIndex.values();
if (null == values) {
@ -143,11 +157,13 @@ public class NotesListAdapter extends CursorAdapter {
return count;
}
// 检查是否全部项目都被选中
public boolean isAllSelected() {
int checkedCount = getSelectedCount();
return (checkedCount != 0 && checkedCount == mNotesCount);
}
// 检查指定位置上的项目是否被选中
public boolean isSelectedItem(final int position) {
if (null == mSelectedIndex.get(position)) {
return false;
@ -155,18 +171,21 @@ 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++) {
@ -176,7 +195,7 @@ public class NotesListAdapter extends CursorAdapter {
mNotesCount++;
}
} else {
Log.e(TAG, "Invalid cursor");
Log.e(TAG, "无效的游标");
return;
}
}

Loading…
Cancel
Save