diff --git a/src/java/net/micode/notes/ui/NotesListAdapter.java b/src/java/net/micode/notes/ui/NotesListAdapter.java index 7695caf..9166fa5 100644 --- a/src/java/net/micode/notes/ui/NotesListAdapter.java +++ b/src/java/net/micode/notes/ui/NotesListAdapter.java @@ -1,273 +1,249 @@ /* - * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * 版权所有 (c) 2010-2011, MiCode 开源社区 (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 + * 本文件授权使用 Apache License, Version 2.0(以下简称“许可证”); + * 除非符合许可证规定,否则不得使用此文件。 + * 您可以从以下网址获取许可证副本: * * 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; - - - /* - * 功能:直译为便签表连接器,继承了CursorAdapter,它为cursor和ListView提供了连接的桥梁。 - * 所以NotesListAdapter实现的是鼠标和编辑便签链接的桥梁 - */ - public class NotesListAdapter extends CursorAdapter { - private static final String TAG = "NotesListAdapter"; - private Context mContext; - private HashMap mSelectedIndex; - private int mNotesCount; //便签数 - private boolean mChoiceMode; //选择模式标记 - - /* - * 桌面widget的属性,包括编号和类型 - */ - public static class AppWidgetAttribute { - public int widgetId; - public int widgetType; - }; - - /* - * 函数功能:初始化便签链接器 - * 函数实现:根据传进来的内容设置相关变量 - */ - public NotesListAdapter(Context context) { - super(context, null); //父类对象置空 - mSelectedIndex = new HashMap(); //新建选项下标的hash表 - mContext = context; - mNotesCount = 0; - } - - @Override - /* - * 函数功能:新建一个视图来存储光标所指向的数据 - * 函数实现:使用兄弟类NotesListItem新建一个项目选项 - */ - public View newView(Context context, Cursor cursor, ViewGroup parent) { - return new NotesListItem(context); - } - - /* - * 函数功能:将已经存在的视图和鼠标指向的数据进行捆绑 - * 函数实现:如下注释 - */ - @Override - public void bindView(View view, Context context, Cursor cursor) { - if (view instanceof NotesListItem) { - //若view是NotesListItem的一个实例 - NoteItemData itemData = new NoteItemData(context, cursor); - ((NotesListItem) view).bind(context, itemData, mChoiceMode, - isSelectedItem(cursor.getPosition())); - //则新建一个项目选项并且用bind跟将view和鼠标,内容,便签数据捆绑在一起 - } - } - - /* - * 函数功能:设置勾选框 - * 函数实现:如下注释 - */ - public void setCheckedItem(final int position, final boolean checked) { - mSelectedIndex.put(position, checked); - //根据定位和是否勾选设置下标 - notifyDataSetChanged(); - //在修改后刷新activity - } - - /* - * 函数功能:判断单选按钮是否勾选 - */ - public boolean isInChoiceMode() { - return mChoiceMode; - } - - /* - * 函数功能:设置单项选项框 - * 函数实现:重置下标并且根据参数mode设置选项 - */ - 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(); - //建立hash表 - 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); - } - //则将id该下标假如选项集合中 - - } - } - - return itemSet; - } - - /* - * 函数功能:建立桌面Widget的选项表 - * 函数实现:如下注释 - */ - public HashSet getSelectedWidget() { - HashSet itemSet = new HashSet(); - for (Integer position : mSelectedIndex.keySet()) { - if (mSelectedIndex.get(position) == true) { - Cursor c = (Cursor) getItem(position); - //以上4句和getSelectedItemIds一样,不再重复 - if (c != null) { - //光标位置可用的话就建立新的Widget属性并编辑下标和类型,最后添加到选项集中 - 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()) { - //若value值为真计数+1 - 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); - } - - @Override - /* - * 函数功能:在activity内容发生局部变动的时候回调该函数计算便签的数量 - * 函数实现:如下注释 - */ - protected void onContentChanged() { - super.onContentChanged(); - //执行基类函数 - calcNotesCount(); - } - - @Override - /* - * 函数功能:在activity光标发生局部变动的时候回调该函数计算便签的数量 - */ - 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++; - //若该位置不为空并且文本类型为便签就+1 - } - } else { - Log.e(TAG, "Invalid cursor"); - return; - } - //否则报错 - } - } - } +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; + +/** + * NotesListAdapter 类用于显示便签列表。 + * 它继承自 CursorAdapter,为数据库中的便签数据和 ListView 提供连接。 + */ +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; + } + + /** + * 构造函数,初始化 NotesListAdapter。 + * @param context 应用上下文。 + */ + public NotesListAdapter(Context context) { + super(context, null); // 父类对象置空 + mSelectedIndex = new HashMap(); // 新建选项下标的哈希表 + mContext = context; + mNotesCount = 0; + } + + @Override + /** + * 创建一个新的视图来存储光标所指向的数据。 + * @param context 应用上下文。 + * @param cursor 数据库光标。 + * @param parent 父视图。 + * @return 新创建的视图。 + */ + public View newView(Context context, Cursor cursor, ViewGroup parent) { + return new NotesListItem(context); + } + + @Override + /** + * 将已经存在的视图和光标指向的数据进行绑定。 + * @param view 视图。 + * @param context 应用上下文。 + * @param cursor 数据库光标。 + */ + 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 true 表示处于选择模式,false 表示不是。 + */ + 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 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; + } + + /** + * 获取选中的小部件属性集合。 + * @return 选中的小部件属性集合。 + */ + 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); + } else { + Log.e(TAG, "Invalid cursor"); + return null; + } + } + } + return itemSet; + } + + /** + * 获取选中的项的数量。 + * @return 选中的项的数量。 + */ + 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; + } + + /** + * 判断是否全部选中。 + * @return true 表示全部选中,false 表示不是。 + */ + public boolean isAllSelected() { + int checkedCount = getSelectedCount(); + return (checkedCount != 0 && checkedCount == mNotesCount); + } + + /** + * 判断是否为选中的项。 + * @param position 位置。 + * @return true 表示是选中的项,false 表示不是。 + */ + 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 + /** + * 在光标发生局部变动时回调该函数计算便签的数量。 + * @param cursor 数据库光标。 + */ + 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; + } + } + } +} \ No newline at end of file