diff --git a/src/net/micode/notes/ui/NotesListAdapter.java b/src/net/micode/notes/ui/NotesListAdapter.java index aa7543b..3588332 100644 --- a/src/net/micode/notes/ui/NotesListAdapter.java +++ b/src/net/micode/notes/ui/NotesListAdapter.java @@ -1,202 +1,296 @@ -/* - * 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 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 NotesListItem(context); - } - - - @Override - public void bindView(View view, Context context, Cursor cursor) { - if (view instanceof NotesListItem) { +package net.micode.notes.ui; - NoteItemData itemData = new NoteItemData(context, cursor); - ((NotesListItem) view).bind(context, itemData, mChoiceMode, - isSelectedItem(cursor.getPosition())); +import android.content.Context; +import android.database.Cursor; +import android.util.Log; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CursorAdapter; - } - } - - public void setCheckedItem(final int position, final boolean checked) { - mSelectedIndex.put(position, checked); +import net.micode.notes.data.Notes; - notifyDataSetChanged(); +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; - } - - - 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); - } - } - } - } - +/** + * @Package: net.micode.notes.ui + * @ClassName: NotesListAdapter + * @Description: + * 直译为便签表连接器,继承了CursorAdapter,它为cursor和ListView提供了连接的桥梁。 + * 所以NotesListAdapter实现的是鼠标和编辑便签链接的桥梁 + * @Author: + * @CreateDate: 12/23/2023 11:35 PM + * @Version: 1.0 + */ +public class NotesListAdapter extends CursorAdapter { + private static final String TAG = "NotesListAdapter"; + private Context mContext; + private HashMap mSelectedIndex; + private int mNotesCount; //便签数 + private boolean mChoiceMode; //选择模式标记 - public HashSet getSelectedItemIds() { - HashSet itemSet = new HashSet(); + /* + * 桌面widget的属性,包括编号和类型 + */ + public static class AppWidgetAttribute { + public int widgetId; + public int widgetType; + }; + /** + * @method NotesListAdapter + * @description + * 初始化便签链接器 + * 函数实现:根据传进来的内容设置相关变量 + * @date: 12/23/2023 11:35 PM + * + * @param context + */ + public NotesListAdapter(Context context) { + super(context, null); //父类对象置空 + mSelectedIndex = new HashMap(); //新建选项下标的hash表 + mContext = context; + mNotesCount = 0; + } - 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; - } - + @Override + /** + * @method newView + * @description + * 新建一个视图来存储光标所指向的数据 + * 通过使用兄弟类NotesListItem新建一个项目选项 + * @date: 12/23/2023 11:35 PM + * + * @param [context, cursor, parent] + * @return android.view.View + */ + public View newView(Context context, Cursor cursor, ViewGroup parent) { + return new NotesListItem(context); + } + @Override + /** + * @method bindView + * @description 将已经存在的视图和鼠标指向的数据进行捆绑 + * @date: 12/23/2023 11:41 PM + * + * @param [view, context, cursor] + * @return void + */ + public void bindView(View view, Context context, Cursor cursor) { + if (view instanceof NotesListItem) { + //若view是NotesListItem的一个实例 + //则新建一个项目选项并且用bind跟将view和鼠标,内容,便签数据捆绑在一起 + NoteItemData itemData = new NoteItemData(context, cursor); + ((NotesListItem) view).bind(context, itemData, mChoiceMode, + isSelectedItem(cursor.getPosition())); + } + } + /** + * @method setCheckedItem + * @description 设置勾选框 + * @date: 12/23/2023 11:41 PM + * + * @param checked + * @param position + * @return void + */ + public void setCheckedItem(final int position, final boolean checked) { + mSelectedIndex.put(position, checked); + //根据定位和是否勾选设置下标 + notifyDataSetChanged(); + //在修改后刷新activity + } - 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; - } + public boolean isInChoiceMode() { + return mChoiceMode; + }//判断单选按钮是否勾选 - 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; - } - + /** + * @method setChoiceMode + * @description 设置单项选项框 + * @date: 12/23/2023 11:40 PM + * + * @param + * @return + */ + public void setChoiceMode(boolean mode) { + mSelectedIndex.clear(); + mChoiceMode = mode; + } - public boolean isAllSelected() { - int checkedCount = getSelectedCount(); - return (checkedCount != 0 && checkedCount == mNotesCount); - } - + /** + * @method selectAll + * @description 选择全部选项 + * @date: 12/23/2023 11:40 PM + * + * @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); + } + } + } + //遍历所有光标可用的位置在判断为便签类型之后勾选单项框 + } + /** + * @method getSelectedItemIds + * @description 建立选择项的下标列表 + * @date: 12/23/2023 11:39 PM + * + * @param + * @return + */ + 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); + } + } + } - public boolean isSelectedItem(final int position) { - if (null == mSelectedIndex.get(position)) { - return false; - } - return mSelectedIndex.get(position); - } - - @Override + return itemSet; + } + /** + * @method getSelectedWidget + * @description 建立桌面Widget的选项表 + * @date: 12/23/2023 11:39 PM + * + * @param + * @return + */ + 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; + } + /** + * @method getSelectedCount + * @description 获取选项个数 + * @date: 12/23/2023 11:39 PM + * + */ + 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++;//若value值为真计数+1 + } + } + return count; + } - protected void onContentChanged() { - super.onContentChanged(); + /** + * @method isAllSelected + * @description 判断是否全部选中 + * @date: 12/23/2023 11:38 PM + * + */ + public boolean isAllSelected() { + int checkedCount = getSelectedCount(); + return (checkedCount != 0 && checkedCount == mNotesCount);//获取选项数看是否等于便签的个数 + } + /** + * @method isSelectedItem + * @description 判断是否为选项表,通过传递的下标来确定 + * @date: 12/23/2023 11:38 PM + * + * @param + * @return + */ + public boolean isSelectedItem(final int position) { + if (null == mSelectedIndex.get(position)) { + return false; + } + return mSelectedIndex.get(position); + } - calcNotesCount(); - } - - @Override - public void changeCursor(Cursor cursor) { - super.changeCursor(cursor); - calcNotesCount(); - } - + @Override + /** + * @method onContentChanged + * @description 在activity内容发生局部变动的时候回调该函数计算便签的数量 + * @date: 12/23/2023 11:38 PM + * + * @param [] + * @return void + */ + protected void onContentChanged() { + super.onContentChanged(); + 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 + @Override + /** + * @method changeCursor + * @description 在activity光标发生局部变动的时候回调该函数计算便签的数量 + * @date: 12/23/2023 11:37 PM + * + * @param [cursor] + * @return void + */ + public void changeCursor(Cursor cursor) { + super.changeCursor(cursor); + calcNotesCount();//执行基类函数 + } + /** + * @method calcNotesCount + * @description 计算便签数量 + * @date: 12/23/2023 11:37 PM + * + * @param + * @return + */ + 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; + } + } + } +} \ No newline at end of file diff --git a/src/net/micode/notes/ui/NotesListItem.java b/src/net/micode/notes/ui/NotesListItem.java index 39868c4..08610b6 100644 --- a/src/net/micode/notes/ui/NotesListItem.java +++ b/src/net/micode/notes/ui/NotesListItem.java @@ -1,131 +1,123 @@ -/* - * 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.text.format.DateUtils; - import android.view.View; - import android.widget.CheckBox; - import android.widget.ImageView; - import android.widget.LinearLayout; - import android.widget.TextView; - - import net.micode.notes.R; - import net.micode.notes.data.Notes; - import net.micode.notes.tool.DataUtils; - import net.micode.notes.tool.ResourceParser.NoteItemBgResources; - - - - public class NotesListItem extends LinearLayout { - private ImageView mAlert; - private TextView mTitle; - private TextView mTime; - private TextView mCallName; - private NoteItemData mItemData; - private CheckBox mCheckBox; - - - public NotesListItem(Context context) { - super(context); - inflate(context, R.layout.note_item, this); - mAlert = (ImageView) findViewById(R.id.iv_alert_icon); - mTitle = (TextView) findViewById(R.id.tv_title); - mTime = (TextView) findViewById(R.id.tv_time); - mCallName = (TextView) findViewById(R.id.tv_name); - mCheckBox = (CheckBox) findViewById(android.R.id.checkbox); - } - - public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) { - if (choiceMode && data.getType() == Notes.TYPE_NOTE) { - mCheckBox.setVisibility(View.VISIBLE); - mCheckBox.setChecked(checked); - } else { - mCheckBox.setVisibility(View.GONE); - } - - mItemData = data; +package net.micode.notes.ui; - if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) { - mCallName.setVisibility(View.GONE); - mAlert.setVisibility(View.VISIBLE); +import android.content.Context; +import android.text.format.DateUtils; +import android.view.View; +import android.widget.CheckBox; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; - mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); +import net.micode.notes.R; +import net.micode.notes.data.Notes; +import net.micode.notes.tool.DataUtils; +import net.micode.notes.tool.ResourceParser.NoteItemBgResources; - mTitle.setText(context.getString(R.string.call_record_folder_name) - + context.getString(R.string.format_folder_files_count, data.getNotesCount())); - mAlert.setImageResource(R.drawable.call_record); - } else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) { - mCallName.setVisibility(View.VISIBLE); - mCallName.setText(data.getCallName()); - mTitle.setTextAppearance(context,R.style.TextAppearanceSecondaryItem); - mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); - if (data.hasAlert()) { - mAlert.setImageResource(R.drawable.clock); - mAlert.setVisibility(View.VISIBLE); - } else { - mAlert.setVisibility(View.GONE); - } - } else { - mCallName.setVisibility(View.GONE); - mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); - - if (data.getType() == Notes.TYPE_FOLDER) { - mTitle.setText(data.getSnippet() - + context.getString(R.string.format_folder_files_count, - data.getNotesCount())); - mAlert.setVisibility(View.GONE); - } else { - mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); - if (data.hasAlert()) { - mAlert.setImageResource(R.drawable.clock); - mAlert.setVisibility(View.VISIBLE); - } else { - mAlert.setVisibility(View.GONE); - } - } - } - - mTime. setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate())); - - setBackground(data); - } - - private void setBackground(NoteItemData data) { - int id = data.getBgColorId(); +/** + * @Package: net.micode.notes.ui + * @ClassName: NotesListItem + * @Description: + * @Author: + * @CreateDate: 12/23/2023 11:35 PM + * @Version: 1.0 + */ +public class NotesListItem extends LinearLayout { + private ImageView mAlert;//闹钟图片 + private TextView mTitle; //标题 + private TextView mTime; //时间 + private TextView mCallName; + private NoteItemData mItemData; //标签数据 + private CheckBox mCheckBox; //打钩框 - if (data.getType() == Notes.TYPE_NOTE) { + /*初始化基本信息*/ + public NotesListItem(Context context) { + super(context); //super()它的主要作用是调整调用父类构造函数的顺序 + inflate(context, R.layout.note_item, this);//Inflate可用于将一个xml中定义的布局控件找出来,这里的xml是r。layout + //findViewById用于从contentView中查找指定ID的View,转换出来的形式根据需要而定; + mAlert = (ImageView) findViewById(R.id.iv_alert_icon); + mTitle = (TextView) findViewById(R.id.tv_title); + mTime = (TextView) findViewById(R.id.tv_time); + mCallName = (TextView) findViewById(R.id.tv_name); + mCheckBox = (CheckBox) findViewById(android.R.id.checkbox); + } + ///根据data的属性对各个控件的属性的控制,主要是可见性Visibility,内容setText,格式setTextAppearance + public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) { + if (choiceMode && data.getType() == Notes.TYPE_NOTE) { + mCheckBox.setVisibility(View.VISIBLE); ///设置可见行为可见 + mCheckBox.setChecked(checked); ///格子打钩 + } else { + mCheckBox.setVisibility(View.GONE); + } - if (data.isSingle() || data.isOneFollowingFolder()) { - setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id)); - } else if (data.isLast()) { - setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id)); - } else if (data.isFirst() || data.isMultiFollowingFolder()) { - setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id)); - } else { - setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id)); - } - } else { + mItemData = data; + ///设置控件属性,一共三种情况,由data的id和父id是否与保存到文件夹的id一致来决定 + if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) { + mCallName.setVisibility(View.GONE); + mAlert.setVisibility(View.VISIBLE); + //设置该textview的style + mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); + //settext为设置内容 + mTitle.setText(context.getString(R.string.call_record_folder_name) + + context.getString(R.string.format_folder_files_count, data.getNotesCount())); + mAlert.setImageResource(R.drawable.call_record); + } else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) { + mCallName.setVisibility(View.VISIBLE); + mCallName.setText(data.getCallName()); + mTitle.setTextAppearance(context,R.style.TextAppearanceSecondaryItem); + mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); + ///关于闹钟的设置 + if (data.hasAlert()) { + mAlert.setImageResource(R.drawable.clock);//图片来源的设置 + mAlert.setVisibility(View.VISIBLE); + } else { + mAlert.setVisibility(View.GONE); + } + } else { + mCallName.setVisibility(View.GONE); + mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); + ///设置title格式 + if (data.getType() == Notes.TYPE_FOLDER) { + mTitle.setText(data.getSnippet() + + context.getString(R.string.format_folder_files_count, + data.getNotesCount())); + mAlert.setVisibility(View.GONE); + } else { + mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); + if (data.hasAlert()) { + mAlert.setImageResource(R.drawable.clock);///设置图片来源 + mAlert.setVisibility(View.VISIBLE); + } else { + mAlert.setVisibility(View.GONE); + } + } + } + ///设置内容,获取相关时间,从data里编辑的日期中获取 + mTime. setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate())); - setBackgroundResource(NoteItemBgResources.getFolderBgRes()); - } - } - public NoteItemData getItemData() { - return mItemData; - } - } \ No newline at end of file + setBackground(data); + } + //根据data的文件属性来设置背景 + private void setBackground(NoteItemData data) { + int id = data.getBgColorId(); + //,若是note型文件,则4种情况,对于4种不同情况的背景来源 + if (data.getType() == Notes.TYPE_NOTE) { + //单个数据并且只有一个子文件夹 + if (data.isSingle() || data.isOneFollowingFolder()) { + setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id)); + } else if (data.isLast()) {//是最后一个数据 + setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id)); + } else if (data.isFirst() || data.isMultiFollowingFolder()) {//是一个数据并有多个子文件夹 + setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id)); + } else { + setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id)); + } + } else { + //若不是note直接调用文件夹的背景来源 + setBackgroundResource(NoteItemBgResources.getFolderBgRes()); + } + } + public NoteItemData getItemData() { + return mItemData; + } +} \ No newline at end of file