Update NotesListAdapter.java

main
Sunique_L 2 years ago
parent c5c75ac2ae
commit 39a013f793

@ -31,154 +31,198 @@ import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
//定义一个公共类NotesListAdapter它继承自CursorAdapter
public class NotesListAdapter extends CursorAdapter { public class NotesListAdapter extends CursorAdapter {
private static final String TAG = "NotesListAdapter"; // 定义一个静态的字符串标签,用于日志记录
private Context mContext; private static final String TAG = "NotesListAdapter";
private HashMap<Integer, Boolean> mSelectedIndex; // 定义一个Context对象用于与应用程序环境交互
private int mNotesCount; private Context mContext;
private boolean mChoiceMode; // 定义一个HashMap对象用于存储选中的项目及其对应的索引
private HashMap<Integer, Boolean> mSelectedIndex;
public static class AppWidgetAttribute { // 定义一个变量,用于存储笔记的数量
public int widgetId; private int mNotesCount;
public int widgetType; // 定义一个布尔变量,表示是否处于选择模式
}; private boolean mChoiceMode;
public NotesListAdapter(Context context) { // 定义一个内部类AppWidgetAttribute包含widgetId和widgetType两个成员变量
super(context, null); public static class AppWidgetAttribute {
mSelectedIndex = new HashMap<Integer, Boolean>(); public int widgetId;
mContext = context; public int widgetType;
mNotesCount = 0; };
}
// 定义一个构造函数接收一个Context对象作为参数
@Override public NotesListAdapter(Context context) {
public View newView(Context context, Cursor cursor, ViewGroup parent) { // 调用父类的构造函数传入上下文、null的游标和null的父视图
return new NotesListItem(context); super(context, null);
} // 初始化mSelectedIndex为空的HashMap对象
mSelectedIndex = new HashMap<Integer, Boolean>();
@Override // 保存传入的上下文对象
public void bindView(View view, Context context, Cursor cursor) { mContext = context;
if (view instanceof NotesListItem) { // 初始化笔记数量为0
NoteItemData itemData = new NoteItemData(context, cursor); mNotesCount = 0;
((NotesListItem) view).bind(context, itemData, mChoiceMode, }
isSelectedItem(cursor.getPosition()));
} // 重写父类的newView方法用于创建新的视图对象
} @Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
public void setCheckedItem(final int position, final boolean checked) { // 返回一个新的NotesListItem对象
mSelectedIndex.put(position, checked); return new NotesListItem(context);
notifyDataSetChanged(); }
}
// 重写父类的bindView方法用于将数据绑定到视图上
public boolean isInChoiceMode() { @Override
return mChoiceMode; public void bindView(View view, Context context, Cursor cursor) {
} // 如果视图是NotesListItem的实例
if (view instanceof NotesListItem) {
public void setChoiceMode(boolean mode) { // 从游标中获取数据并创建一个新的NoteItemData对象
mSelectedIndex.clear(); NoteItemData itemData = new NoteItemData(context, cursor);
mChoiceMode = mode; // 将数据绑定到视图中,同时传入选择模式和当前项是否被选中的信息
} ((NotesListItem) view).bind(context, itemData, mChoiceMode,
isSelectedItem(cursor.getPosition()));
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) { public void setCheckedItem(final int position, final boolean checked) {
setCheckedItem(i, checked); // 在mSelectedIndex中添加或修改对应位置的选中状态
} mSelectedIndex.put(position, checked);
} // 通知数据集已改变,触发视图更新
} notifyDataSetChanged();
} }
public HashSet<Long> getSelectedItemIds() {
HashSet<Long> itemSet = new HashSet<Long>(); //判断当前是否处于选择模式
for (Integer position : mSelectedIndex.keySet()) { public boolean isInChoiceMode() {
if (mSelectedIndex.get(position) == true) { return mChoiceMode;
Long id = getItemId(position); }
if (id == Notes.ID_ROOT_FOLDER) {
Log.d(TAG, "Wrong item id, should not happen"); //设置选择模式,清空已选中的项目并改变选择模式
} else { public void setChoiceMode(boolean mode) {
itemSet.add(id); mSelectedIndex.clear(); // 清空已选中的项目
} mChoiceMode = mode; // 设置新的选择模式
} }
}
//全选或全不选
return itemSet; public void selectAll(boolean checked) {
} Cursor cursor = getCursor(); // 获取游标,游标用于遍历查询结果
for (int i = 0; i < getCount(); i++) { // 遍历所有项目
public HashSet<AppWidgetAttribute> getSelectedWidget() { if (cursor.moveToPosition(i)) { // 移动游标到当前位置
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>(); if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { // 如果当前项是笔记类型
for (Integer position : mSelectedIndex.keySet()) { setCheckedItem(i, checked); // 设置该项是否被选中
if (mSelectedIndex.get(position) == true) { }
Cursor c = (Cursor) getItem(position); }
if (c != null) { }
AppWidgetAttribute widget = new AppWidgetAttribute(); }
NoteItemData item = new NoteItemData(mContext, c);
widget.widgetId = item.getWidgetId(); //获取所有被选中的项目的id并返回一个HashSet集合
widget.widgetType = item.getWidgetType(); public HashSet<Long> getSelectedItemIds() {
itemSet.add(widget); HashSet<Long> itemSet = new HashSet<Long>(); // 创建一个HashSet集合用于存放被选中的项目id
/** for (Integer position : mSelectedIndex.keySet()) { // 遍历所有被选中的项目的位置
* Don't close cursor here, only the adapter could close it if (mSelectedIndex.get(position) == true) { // 如果该位置的项被选中
*/ Long id = getItemId(position); // 获取该位置的项目id
} else { if (id == Notes.ID_ROOT_FOLDER) { // 如果获取的id是根文件夹的id则记录一个错误日志因为根文件夹不应该被选中
Log.e(TAG, "Invalid cursor"); Log.d(TAG, "Wrong item id, should not happen");
return null; } else {
} itemSet.add(id); // 否则将该id添加到HashSet集合中
} }
} }
return itemSet; }
} return itemSet; // 返回HashSet集合
}
public int getSelectedCount() {
Collection<Boolean> values = mSelectedIndex.values();
if (null == values) { //获取所有选中的小部件属性
return 0; public HashSet<AppWidgetAttribute> getSelectedWidget() {
} HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>(); // 创建一个HashSet集合用于存放选中的小部件属性
Iterator<Boolean> iter = values.iterator(); for (Integer position : mSelectedIndex.keySet()) { // 遍历所有被选中的项目的位置
int count = 0; if (mSelectedIndex.get(position) == true) { // 如果该位置的项被选中
while (iter.hasNext()) { Cursor c = (Cursor) getItem(position); // 获取该位置的项目以Cursor对象的形式返回
if (true == iter.next()) { if (c != null) { // 如果Cursor不为空
count++; AppWidgetAttribute widget = new AppWidgetAttribute(); // 创建一个新的AppWidgetAttribute对象
} NoteItemData item = new NoteItemData(mContext, c); // 使用Cursor对象创建一个NoteItemData对象
} widget.widgetId = item.getWidgetId(); // 从NoteItemData对象获取小部件ID并设置到AppWidgetAttribute对象中
return count; widget.widgetType = item.getWidgetType(); // 从NoteItemData对象获取小部件类型并设置到AppWidgetAttribute对象中
} itemSet.add(widget); // 将创建的AppWidgetAttribute对象添加到HashSet集合中
/**
public boolean isAllSelected() { * Cursor
int checkedCount = getSelectedCount(); */
return (checkedCount != 0 && checkedCount == mNotesCount); } else {
} Log.e(TAG, "Invalid cursor"); // 如果Cursor为空记录错误日志
return null; // 返回null表示获取失败
public boolean isSelectedItem(final int position) { }
if (null == mSelectedIndex.get(position)) { }
return false; }
} return itemSet; // 返回HashSet集合包含所有选中的小部件属性
return mSelectedIndex.get(position); }
}
//获取被选中的项目数量
@Override public int getSelectedCount() {
protected void onContentChanged() { Collection<Boolean> values = mSelectedIndex.values(); // 获取所有被选中的项目(布尔值)的集合
super.onContentChanged(); if (null == values) { // 如果集合为空(没有被选中的项目)
calcNotesCount(); return 0; // 返回0表示被选中的项目数量为0
} }
Iterator<Boolean> iter = values.iterator(); // 创建一个迭代器来遍历布尔值的集合
@Override int count = 0; // 初始化计数器为0
public void changeCursor(Cursor cursor) { while (iter.hasNext()) { // 当还有下一个布尔值时
super.changeCursor(cursor); if (true == iter.next()) { // 如果布尔值为true表示被选中
calcNotesCount(); count++; // 计数器加1
} }
}
private void calcNotesCount() { return count; // 返回被选中的项目数量
mNotesCount = 0; }
for (int i = 0; i < getCount(); i++) {
Cursor c = (Cursor) getItem(i); //检查是否所有项目都被选中
if (c != null) { public boolean isAllSelected() {
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) { int checkedCount = getSelectedCount(); // 获取被选中的项目数量
mNotesCount++; return (checkedCount != 0 && checkedCount == mNotesCount); // 如果被选中的项目数量不为0且等于总的项目数量则返回true表示所有项目都被选中否则返回false。
} }
} else {
Log.e(TAG, "Invalid cursor"); //定义一个方法,用于判断指定位置的项目是否被选中
return; public boolean isSelectedItem(final int position) {
} // 如果指定位置的项目在mSelectedIndex映射中对应的值为null返回false表示未被选中
} if (null == mSelectedIndex.get(position)) {
} return false;
}
// 返回mSelectedIndex映射中指定位置对应的值如果为true则表示被选中为false则表示未被选中
return mSelectedIndex.get(position);
}
//重写onContentChanged方法该方法在内容发生变化时被调用
@Override
protected void onContentChanged() {
// 调用父类的onContentChanged方法执行默认的内容变化处理
super.onContentChanged();
// 调用calcNotesCount方法重新计算笔记数量
calcNotesCount();
}
//重写changeCursor方法该方法在Cursor发生变化时被调用
@Override
public void changeCursor(Cursor cursor) {
// 调用父类的changeCursor方法执行默认的Cursor变化处理
super.changeCursor(cursor);
// 调用calcNotesCount方法重新计算笔记数量
calcNotesCount();
}
//定义一个私有方法,用于计算笔记数量
private void calcNotesCount() {
// 初始化笔记数量为0
mNotesCount = 0;
// 遍历所有的项目从0到getCount()-1
for (int i = 0; i < getCount(); i++) {
// 获取指定位置的项目以Cursor对象的形式返回
Cursor c = (Cursor) getItem(i);
// 如果Cursor不为空
if (c != null) {
// 如果当前项目的类型为“笔记”TYPE_NOTE则笔记数量加1
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {
mNotesCount++;
}
} else {
// 如果Cursor为空记录错误日志并退出方法
Log.e(TAG, "Invalid cursor");
return;
}
}
} }
Loading…
Cancel
Save