java/net/micode/notes/ui/NotesListAdapter.java

duan
dxj 2 months ago
parent 6d1cc27695
commit d0e9099c79

@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* <url id="d0s4mbuf5ku6itl3bdkg" type="url" status="parsed" title="Apache License, Version 2.0" wc="10467">http://www.apache.org/licenses/LICENSE-2.0</url>
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@ -38,11 +38,13 @@ public class NotesListAdapter extends CursorAdapter {
private int mNotesCount;
private boolean mChoiceMode;
// 定义应用小部件属性类
public static class AppWidgetAttribute {
public int widgetId;
public int widgetType;
};
// 构造函数
public NotesListAdapter(Context context) {
super(context, null);
mSelectedIndex = new HashMap<Integer, Boolean>();
@ -52,43 +54,47 @@ public class NotesListAdapter extends CursorAdapter {
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return new NotesListItem(context);
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()));
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();
mSelectedIndex.put(position, checked); // 更新选中状态
notifyDataSetChanged(); // 通知数据已更改
}
// 判断是否处于选择模式
public boolean isInChoiceMode() {
return mChoiceMode;
}
// 设置选择模式
public void setChoiceMode(boolean mode) {
mSelectedIndex.clear();
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);
if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { // 如果是笔记类型
setCheckedItem(i, checked); // 设置选中状态
}
}
}
}
// 获取选中的项 ID 集合
public HashSet<Long> getSelectedItemIds() {
HashSet<Long> itemSet = new HashSet<Long>();
for (Integer position : mSelectedIndex.keySet()) {
@ -97,7 +103,7 @@ public class NotesListAdapter extends CursorAdapter {
if (id == Notes.ID_ROOT_FOLDER) {
Log.d(TAG, "Wrong item id, should not happen");
} else {
itemSet.add(id);
itemSet.add(id); // 添加选中的项 ID
}
}
}
@ -105,6 +111,7 @@ public class NotesListAdapter extends CursorAdapter {
return itemSet;
}
// 获取选中的小部件属性集合
public HashSet<AppWidgetAttribute> getSelectedWidget() {
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
for (Integer position : mSelectedIndex.keySet()) {
@ -113,12 +120,9 @@ public class NotesListAdapter extends CursorAdapter {
if (c != null) {
AppWidgetAttribute widget = new AppWidgetAttribute();
NoteItemData item = new NoteItemData(mContext, c);
widget.widgetId = item.getWidgetId();
widget.widgetType = item.getWidgetType();
itemSet.add(widget);
/**
* Don't close cursor here, only the adapter could close it
*/
widget.widgetId = item.getWidgetId(); // 获取小部件 ID
widget.widgetType = item.getWidgetType(); // 获取小部件类型
itemSet.add(widget); // 添加小部件属性
} else {
Log.e(TAG, "Invalid cursor");
return null;
@ -128,6 +132,7 @@ public class NotesListAdapter extends CursorAdapter {
return itemSet;
}
// 获取选中的项数
public int getSelectedCount() {
Collection<Boolean> values = mSelectedIndex.values();
if (null == values) {
@ -143,11 +148,13 @@ public class NotesListAdapter extends CursorAdapter {
return count;
}
// 判断是否全部选中
public boolean isAllSelected() {
int checkedCount = getSelectedCount();
return (checkedCount != 0 && checkedCount == mNotesCount);
return (checkedCount != 0 && checkedCount == mNotesCount); // 如果选中数等于笔记数,则全部选中
}
// 判断项是否被选中
public boolean isSelectedItem(final int position) {
if (null == mSelectedIndex.get(position)) {
return false;
@ -158,21 +165,22 @@ public class NotesListAdapter extends CursorAdapter {
@Override
protected void onContentChanged() {
super.onContentChanged();
calcNotesCount();
calcNotesCount(); // 计算笔记数量
}
@Override
public void changeCursor(Cursor cursor) {
super.changeCursor(cursor);
calcNotesCount();
calcNotesCount(); // 更改游标并计算笔记数量
}
// 计算笔记数量
private void calcNotesCount() {
mNotesCount = 0;
for (int i = 0; i < getCount(); i++) {
Cursor c = (Cursor) getItem(i);
if (c != null) {
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) { // 如果是笔记类型
mNotesCount++;
}
} else {

Loading…
Cancel
Save