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.
rjgc/ui/NotesListAdapter.java

231 lines
7.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.ui.NotesListItem.NoteItemData;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
// ... existing code ...
/**
* 笔记列表适配器
*
* 【功能说明】
* 1. 继承CursorAdapter用于在ListView中显示笔记列表
* 2. 支持多选模式,可批量操作笔记
* 3. 管理选中状态和桌面小部件信息
* 4. 自动统计笔记和文件夹数量
*/
public class NotesListAdapter extends CursorAdapter {
private Context mContext;
private Map<Long, Boolean> mSelectedIndex;
private int mChoiceMode;
private int mCount;
/**
* 桌面小部件属性类
* 用于存储小部件的ID和类型信息
*/
public static class AppWidgetAttribute {
public int widgetId;
public int widgetType;
}
// 这里的索引顺序必须和 PROJECTION 数组完全一致
private static final int ID = 0;
private static final int SNIPPET = 1;
private static final int TYPE = 2;
private static final int MODIFIED_DATE = 3;
private static final int ALERT_DATE = 4;
private static final int BG_COLOR_ID = 5;
private static final int PARENT_ID = 7;
// 查询投影:包含笔记列表显示所需的所有字段
public static final String[] PROJECTION = {
NoteColumns.ID,
NoteColumns.SNIPPET,
NoteColumns.TYPE,
NoteColumns.MODIFIED_DATE,
NoteColumns.ALERTED_DATE,
NoteColumns.BG_COLOR_ID,
NoteColumns.HAS_ATTACHMENT,
NoteColumns.PARENT_ID
};
/**
* 构造函数
* @param context 上下文对象
*/
public NotesListAdapter(Context context) {
super(context, null);
mSelectedIndex = new HashMap<>();
mContext = context;
mChoiceMode = 0;
}
@Override
public void changeCursor(Cursor cursor) {
super.changeCursor(cursor);
mCount = 0;
if (cursor != null) {
mCount = cursor.getCount();
}
// 切换游标时清空选中状态
mSelectedIndex.clear();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return new NotesListItem(mContext);
}
@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 == ListView.CHOICE_MODE_MULTIPLE,
mSelectedIndex.containsKey(itemData.getId()) ? mSelectedIndex.get(itemData.getId()) : false);
}
}
/**
* 设置指定位置的笔记为选中或非选中状态
* @param position 列表位置
* @param checked true表示选中false表示取消选中
*/
public void setCheckedItem(final int position, final boolean checked) {
Cursor c = getCursor();
if (c.moveToPosition(position)) {
long id = c.getLong(ID);
if (id >= 0) {
mSelectedIndex.put(id, checked);
}
}
notifyDataSetChanged();
}
/**
* 设置选择模式
* @param mode 选择模式(单选/多选/无)
*/
public void setChoiceMode(int mode) {
mChoiceMode = mode;
// 如果不是多选模式,清空选中状态
if (mChoiceMode != ListView.CHOICE_MODE_MULTIPLE) {
mSelectedIndex.clear();
}
}
/**
* 判断是否所有笔记都被选中
* @return true表示全部选中
*/
public boolean isAllSelected() {
int selectedCount = mSelectedIndex.size();
return selectedCount == mCount && selectedCount > 0;
}
/**
* 全选或全不选
* @param checked true表示全选false表示全不选
*/
public void selectAll(boolean checked) {
Cursor c = getCursor();
mSelectedIndex.clear();
if (c != null) {
for (int i = 0; i < c.getCount(); i++) {
if (c.moveToPosition(i)) {
long id = c.getLong(ID);
if (id >= 0) {
mSelectedIndex.put(id, checked);
}
}
}
}
notifyDataSetChanged();
}
/**
* 获取已选中的笔记数量
* @return 选中数量
*/
public int getSelectedCount() {
return mSelectedIndex.size();
}
/**
* 获取所有已选中笔记的ID集合
* @return 笔记ID集合
*/
public HashSet<Long> getSelectedItemIds() {
HashSet<Long> ids = new HashSet<>();
for (Map.Entry<Long, Boolean> entry : mSelectedIndex.entrySet()) {
if (entry.getValue()) {
ids.add(entry.getKey());
}
}
return ids;
}
/**
* 获取所有已选中笔记关联的桌面小部件信息
* @return 小部件属性集合
*/
public HashSet<AppWidgetAttribute> getSelectedWidget() {
HashSet<AppWidgetAttribute> widgets = new HashSet<>();
Cursor c = getCursor();
if (c != null) {
int widgetIdIdx = c.getColumnIndex(NoteColumns.WIDGET_ID);
int widgetTypeIdx = c.getColumnIndex(NoteColumns.TYPE);
for (int i = 0; i < c.getCount(); i++) {
if (c.moveToPosition(i)) {
long id = c.getLong(ID);
if (mSelectedIndex.containsKey(id) && mSelectedIndex.get(id)) {
AppWidgetAttribute widget = new AppWidgetAttribute();
if (widgetIdIdx >= 0) widget.widgetId = c.getInt(widgetIdIdx);
if (widgetTypeIdx >= 0) widget.widgetType = c.getInt(widgetTypeIdx);
widgets.add(widget);
}
}
}
}
return widgets;
}
@Override
public NoteItemData getItem(int position) {
Cursor c = getCursor();
if (c.moveToPosition(position)) {
return new NoteItemData(mContext, c);
}
return null;
}
}