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.
software/NotesListAdapter.txt

219 lines
6.1 KiB

/**
* 便签列表适配器,用于管理和显示便签列表的数据。
*/
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; // 小部件ID
public int widgetType; // 小部件类型
};
/**
* 构造函数。
* @param context 上下文对象
*/
public NotesListAdapter(Context context) {
super(context, null);
mSelectedIndex = new HashMap<Integer, Boolean>();
mContext = context;
mNotesCount = 0;
}
/**
* 创建新的列表项视图。
* @param context 上下文对象
* @param cursor 游标对象
* @param parent 父视图组
* @return 新的列表项视图
*/
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return new NotesListItem(context);
}
/**
* 绑定数据到列表项视图。
* @param view 列表项视图
* @param context 上下文对象
* @param cursor 游标对象
*/
@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()));
}
}
/**
* 设置列表项的选中状态。
* @param position 列表项位置
* @param checked 是否选中
*/
public void setCheckedItem(final int position, final boolean checked) {
mSelectedIndex.put(position, checked);
notifyDataSetChanged();
}
/**
* 获取是否处于选择模式。
* @return 是否处于选择模式
*/
public boolean isInChoiceMode() {
return mChoiceMode;
}
/**
* 设置选择模式。
* @param mode 是否开启选择模式
*/
public void setChoiceMode(boolean mode) {
mSelectedIndex.clear();
mChoiceMode = mode;
}
/**
* 全选或全不选。
* @param checked 是否选中
*/
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集合。
* @return 选中的便签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");
} else {
itemSet.add(id);
}
}
}
return itemSet;
}
/**
* 获取选中的小部件属性集合。
* @return 选中的小部件属性集合
*/
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);
} else {
Log.e(TAG, "Invalid cursor");
return null;
}
}
}
return itemSet;
}
/**
* 获取选中数量。
* @return 选中数量
*/
public int getSelectedCount() {
Collection<Boolean> values = mSelectedIndex.values();
if (null == values) {
return 0;
}
Iterator<Boolean> iter = values.iterator();
int count = 0;
while (iter.hasNext()) {
if (true == iter.next()) {
count++;
}
}
return count;
}
/**
* 判断是否全部选中。
* @return 是否全部选中
*/
public boolean isAllSelected() {
int checkedCount = getSelectedCount();
return (checkedCount != 0 && checkedCount == mNotesCount);
}
/**
* 判断指定位置的项是否选中。
* @param position 项的位置
* @return 是否选中
*/
public boolean isSelectedItem(final int position) {
if (null == mSelectedIndex.get(position)) {
return false;
}
return mSelectedIndex.get(position);
}
/**
* 当内容变化时调用。
*/
@Override
protected void onContentChanged() {
super.onContentChanged();
calcNotesCount();
}
/**
* 更改游标。
* @param cursor 新的游标
*/
@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;
}
}
}
}