You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
5.6 KiB
174 lines
5.6 KiB
/*
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* 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
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package net.micode.notes.ui;
|
|
|
|
import android.content.Context;
|
|
import android.database.Cursor;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.CursorAdapter;
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
import java.util.Collection;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.Iterator;
|
|
|
|
|
|
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; // 选择模式标志
|
|
|
|
// AppWidgetAttribute 内部类,用于存储应用 Widget 的属性
|
|
public static class AppWidgetAttribute {
|
|
public int widgetId; // Widget ID
|
|
public int widgetType; // Widget 类型
|
|
};
|
|
|
|
// 构造函数
|
|
public NotesListAdapter(Context context) {
|
|
super(context, null); // 调用父类的构造函数
|
|
mSelectedIndex = new HashMap<Integer, Boolean>();
|
|
mContext = context;
|
|
mNotesCount = 0;
|
|
}
|
|
|
|
// 实现 CursorAdapter 的新视图创建方法
|
|
@Override
|
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
|
return new NotesListItem(context); // 返回自定义的列表项视图
|
|
}
|
|
|
|
// 实现 CursorAdapter 的绑定视图方法
|
|
@Override
|
|
public void bindView(View view, Context context, Cursor cursor) {
|
|
if (view instanceof NotesListItem) {
|
|
NoteItemData itemData = new NoteItemData(context, cursor); // 创建 NoteItemData 实例
|
|
((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++) {
|
|
if (cursor.moveToPosition(i)) {
|
|
if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) {
|
|
setCheckedItem(i, checked); // 设置选中状态
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 获取所有选中项的 ID
|
|
public HashSet<Long> getSelectedItemIds() {
|
|
HashSet<Long> itemSet = new HashSet<Long>();
|
|
// 遍历 mSelectedIndex 并添加选中项的 ID
|
|
return itemSet;
|
|
}
|
|
|
|
public HashSet<AppWidgetAttribute> getSelectedWidget() {
|
|
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
|
|
for (Integer position : mSelectedIndex.keySet()) {
|
|
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();
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
return itemSet;
|
|
}
|
|
|
|
// 检查是否所有项都已选中
|
|
public boolean isAllSelected() {
|
|
int checkedCount = getSelectedCount();
|
|
return (checkedCount != 0 && checkedCount == mNotesCount);
|
|
}
|
|
|
|
// 检查指定位置的项是否被选中
|
|
public boolean isSelectedItem(final int position) {
|
|
if (null == mSelectedIndex.get(position)) {
|
|
return false;
|
|
}
|
|
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++) {
|
|
Cursor c = (Cursor) getItem(i);
|
|
if (c != null) {
|
|
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {
|
|
mNotesCount++;
|
|
}
|
|
} else {
|
|
Log.e(TAG, "Invalid cursor");
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
} |