/* * 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. */ /*这段代码展示了一个自定义的 CursorAdapter 类 NotesListAdapter 的实现,主要用于显示一个包含笔记列表的适配器。这个适配器支持选择模式(choice mode),允许用户多选笔记条目进行批量操作。*/ 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 mSelectedIndex; 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(); mContext = context; mNotesCount = 0; } /*构造函数初始化了适配器的基本属性,包括上下文、存储选定索引的哈希表和笔记数量*/ @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return new NotesLis tItem(context); } /*此方法负责创建一个新的视图,这里返回的是一个 NotesListItem 视图对象*/ @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())); } } /*此方法用于绑定数据到视图。这里将从 Cursor 中获取的数据绑定到 NotesListItem 对象上,并设置选择模式和当前项是否被选中*/ 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); } } } } /*此方法遍历所有的笔记,并根据传入的布尔值设置所有笔记项为选中或取消选中*/ public HashSet getSelectedItemIds() { HashSet itemSet = new HashSet(); 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; } /*此方法返回一个包含所有被选中项ID的集合*/ public HashSet getSelectedWidget() { HashSet itemSet = new HashSet(); 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 int getSelectedCount() { Collection values = mSelectedIndex.values(); if (null == values) { return 0; } Iterator iter = values.iterator(); int count = 0; while (iter.hasNext()) { if (true == iter.next()) { count++; } } return count; } /*此方法计算并返回被选中项的数量*/ 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); } /*setCheckedItem 方法用于设置指定位置的项是否被选中,并通知适配器数据已改变。isSelectedItem 方法则用于检查指定位置的项是否已被选中*/ @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; } } } } /*此方法用于计算笔记的数量,只统计类型为 Notes.TYPE_NOTE 的条目*/