刘瑞泽
11111 2 years ago
parent b3e78ab3b7
commit 1b798d3ba0

@ -1,202 +1,296 @@
/* package net.micode.notes.ui;
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* import android.content.Context;
* Licensed under the Apache License, Version 2.0 (the "License"); import android.database.Cursor;
* you may not use this file except in compliance with the License. import android.util.Log;
* You may obtain a copy of the License at import android.view.View;
* import android.view.ViewGroup;
* http://www.apache.org/licenses/LICENSE-2.0 import android.widget.CursorAdapter;
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, import net.micode.notes.data.Notes;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and import java.util.Collection;
* limitations under the License. import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
/**
* @Package: net.micode.notes.ui
* @ClassName: NotesListAdapter
* @Description:
* 便CursorAdaptercursorListView
* NotesListAdapter便
* @Author:
* @CreateDate: 12/23/2023 11:35 PM
* @Version: 1.0
*/ */
public class NotesListAdapter extends CursorAdapter {
package net.micode.notes.ui; private static final String TAG = "NotesListAdapter";
private Context mContext;
import android.content.Context; private HashMap<Integer, Boolean> mSelectedIndex;
import android.database.Cursor; private int mNotesCount; //便签数
import android.util.Log; private boolean mChoiceMode; //选择模式标记
import android.view.View;
import android.view.ViewGroup; /*
import android.widget.CursorAdapter; * widget
*/
public static class AppWidgetAttribute {
import net.micode.notes.data.Notes; public int widgetId;
public int widgetType;
import java.util.Collection; };
import java.util.HashMap; /**
import java.util.HashSet; * @method NotesListAdapter
import java.util.Iterator; * @description
* 便
*
public class NotesListAdapter extends CursorAdapter { * @date: 12/23/2023 11:35 PM
private static final String TAG = "NotesListAdapter"; *
private Context mContext; * @param context
private HashMap<Integer, Boolean> mSelectedIndex; */
private int mNotesCount; public NotesListAdapter(Context context) {
private boolean mChoiceMode; super(context, null); //父类对象置空
mSelectedIndex = new HashMap<Integer, Boolean>(); //新建选项下标的hash表
public static class AppWidgetAttribute { mContext = context;
public int widgetId; mNotesCount = 0;
public int widgetType; }
};
@Override
/**
public NotesListAdapter(Context context) { * @method newView
super(context, null); * @description
mSelectedIndex = new HashMap<Integer, Boolean>(); *
mContext = context; * 使NotesListItem
mNotesCount = 0; * @date: 12/23/2023 11:35 PM
} *
* @param [context, cursor, parent]
@Override * @return android.view.View
*/
public View newView(Context context, Cursor cursor, ViewGroup parent) { public View newView(Context context, Cursor cursor, ViewGroup parent) {
return new NotesListItem(context); return new NotesListItem(context);
} }
@Override
/**
@Override * @method bindView
public void bindView(View view, Context context, Cursor cursor) { * @description
if (view instanceof NotesListItem) { * @date: 12/23/2023 11:41 PM
*
NoteItemData itemData = new NoteItemData(context, cursor); * @param [view, context, cursor]
((NotesListItem) view).bind(context, itemData, mChoiceMode, * @return void
isSelectedItem(cursor.getPosition())); */
public void bindView(View view, Context context, Cursor cursor) {
} if (view instanceof NotesListItem) {
} //若view是NotesListItem的一个实例
//则新建一个项目选项并且用bind跟将view和鼠标内容便签数据捆绑在一起
NoteItemData itemData = new NoteItemData(context, cursor);
public void setCheckedItem(final int position, final boolean checked) { ((NotesListItem) view).bind(context, itemData, mChoiceMode,
mSelectedIndex.put(position, checked); isSelectedItem(cursor.getPosition()));
}
notifyDataSetChanged(); }
/**
} * @method setCheckedItem
* @description
* @date: 12/23/2023 11:41 PM
public boolean isInChoiceMode() { *
return mChoiceMode; * @param checked
} * @param position
* @return void
*/
public void setChoiceMode(boolean mode) { public void setCheckedItem(final int position, final boolean checked) {
mSelectedIndex.clear(); mSelectedIndex.put(position, checked);
mChoiceMode = mode; //根据定位和是否勾选设置下标
} notifyDataSetChanged();
//在修改后刷新activity
}
public void selectAll(boolean checked) {
Cursor cursor = getCursor(); public boolean isInChoiceMode() {
for (int i = 0; i < getCount(); i++) { return mChoiceMode;
if (cursor.moveToPosition(i)) { }//判断单选按钮是否勾选
if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) {
setCheckedItem(i, checked); /**
} * @method setChoiceMode
} * @description
} * @date: 12/23/2023 11:40 PM
} *
* @param
* @return
public HashSet<Long> getSelectedItemIds() { */
HashSet<Long> itemSet = new HashSet<Long>(); public void setChoiceMode(boolean mode) {
mSelectedIndex.clear();
for (Integer position : mSelectedIndex.keySet()) { mChoiceMode = mode;
if (mSelectedIndex.get(position) == true) { }
Long id = getItemId(position);
if (id == Notes.ID_ROOT_FOLDER) { /**
Log.d(TAG, "Wrong item id, should not happen"); * @method selectAll
} else { * @description
itemSet.add(id); * @date: 12/23/2023 11:40 PM
} *
} * @param checked
} */
public void selectAll(boolean checked) {
return itemSet; Cursor cursor = getCursor();//获取光标位置
} for (int i = 0; i < getCount(); i++) {
if (cursor.moveToPosition(i)) {
if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) {
public HashSet<AppWidgetAttribute> getSelectedWidget() { setCheckedItem(i, checked);
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); * @method getSelectedItemIds
widget.widgetId = item.getWidgetId(); * @description
widget.widgetType = item.getWidgetType(); * @date: 12/23/2023 11:39 PM
itemSet.add(widget); *
} else { * @param
Log.e(TAG, "Invalid cursor"); * @return
return null; */
} public HashSet<Long> getSelectedItemIds() {
} HashSet<Long> itemSet = new HashSet<Long>();//建立hash表
} for (Integer position : mSelectedIndex.keySet()) {
return itemSet; //遍历所有的关键
} if (mSelectedIndex.get(position) == true) {
//若光标位置可用
public int getSelectedCount() { Long id = getItemId(position);
Collection<Boolean> values = mSelectedIndex.values(); if (id == Notes.ID_ROOT_FOLDER) {
if (null == values) { //原文件不需要添加
return 0; Log.d(TAG, "Wrong item id, should not happen");
} } else {
Iterator<Boolean> iter = values.iterator(); itemSet.add(id);
int count = 0; }
while (iter.hasNext()) { }
if (true == iter.next()) { }
count++;
} return itemSet;
} }
return count; /**
} * @method getSelectedWidget
* @description Widget
* @date: 12/23/2023 11:39 PM
public boolean isAllSelected() { *
int checkedCount = getSelectedCount(); * @param
return (checkedCount != 0 && checkedCount == mNotesCount); * @return
} */
public HashSet<AppWidgetAttribute> getSelectedWidget() {
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
public boolean isSelectedItem(final int position) { for (Integer position : mSelectedIndex.keySet()) {
if (null == mSelectedIndex.get(position)) { if (mSelectedIndex.get(position) == true) {
return false; Cursor c = (Cursor) getItem(position);
} //以上4句和getSelectedItemIds一样不再重复
return mSelectedIndex.get(position); if (c != null) {
} //光标位置可用的话就建立新的Widget属性并编辑下标和类型最后添加到选项集中
AppWidgetAttribute widget = new AppWidgetAttribute();
@Override NoteItemData item = new NoteItemData(mContext, c);
widget.widgetId = item.getWidgetId();
widget.widgetType = item.getWidgetType();
protected void onContentChanged() { itemSet.add(widget);
super.onContentChanged(); /**
* Don't close cursor here, only the adapter could close it
calcNotesCount(); */
} } else {
Log.e(TAG, "Invalid cursor");
@Override return null;
public void changeCursor(Cursor cursor) { }
super.changeCursor(cursor); }
calcNotesCount(); }
} return itemSet;
}
private void calcNotesCount() { /**
mNotesCount = 0; * @method getSelectedCount
for (int i = 0; i < getCount(); i++) { * @description
Cursor c = (Cursor) getItem(i); * @date: 12/23/2023 11:39 PM
if (c != null) { *
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) { */
mNotesCount++; public int getSelectedCount() {
} Collection<Boolean> values = mSelectedIndex.values();//首先获取选项下标的值
} else { if (null == values) {
Log.e(TAG, "Invalid cursor"); return 0;
return; }
} Iterator<Boolean> iter = values.iterator();//初始化叠加器
} int count = 0;
} while (iter.hasNext()) {
} if (true == iter.next()) {
count++;//若value值为真计数+1
}
}
return count;
}
/**
* @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);
}
@Override
/**
* @method onContentChanged
* @description activity便
* @date: 12/23/2023 11:38 PM
*
* @param []
* @return void
*/
protected void onContentChanged() {
super.onContentChanged();
calcNotesCount();//执行基类函数
}
@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;
}
}
}
}

@ -1,131 +1,123 @@
/* package net.micode.notes.ui;
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* import android.content.Context;
* Licensed under the Apache License, Version 2.0 (the "License"); import android.text.format.DateUtils;
* you may not use this file except in compliance with the License. import android.view.View;
* You may obtain a copy of the License at import android.widget.CheckBox;
* import android.widget.ImageView;
* http://www.apache.org/licenses/LICENSE-2.0 import android.widget.LinearLayout;
* import android.widget.TextView;
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, import net.micode.notes.R;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. import net.micode.notes.data.Notes;
* See the License for the specific language governing permissions and import net.micode.notes.tool.DataUtils;
* limitations under the License. import net.micode.notes.tool.ResourceParser.NoteItemBgResources;
/**
* @Package: net.micode.notes.ui
* @ClassName: NotesListItem
* @Description:
* @Author:
* @CreateDate: 12/23/2023 11:35 PM
* @Version: 1.0
*/ */
public class NotesListItem extends LinearLayout {
package net.micode.notes.ui; private ImageView mAlert;//闹钟图片
private TextView mTitle; //标题
import android.content.Context; private TextView mTime; //时间
import android.text.format.DateUtils; private TextView mCallName;
import android.view.View; private NoteItemData mItemData; //标签数据
import android.widget.CheckBox; private CheckBox mCheckBox; //打钩框
import android.widget.ImageView;
import android.widget.LinearLayout; /*初始化基本信息*/
import android.widget.TextView; public NotesListItem(Context context) {
super(context); //super()它的主要作用是调整调用父类构造函数的顺序
import net.micode.notes.R; inflate(context, R.layout.note_item, this);//Inflate可用于将一个xml中定义的布局控件找出来,这里的xml是r。layout
import net.micode.notes.data.Notes; //findViewById用于从contentView中查找指定ID的View转换出来的形式根据需要而定;
import net.micode.notes.tool.DataUtils; mAlert = (ImageView) findViewById(R.id.iv_alert_icon);
import net.micode.notes.tool.ResourceParser.NoteItemBgResources; 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 class NotesListItem extends LinearLayout { }
private ImageView mAlert; ///根据data的属性对各个控件的属性的控制主要是可见性Visibility内容setText格式setTextAppearance
private TextView mTitle; public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
private TextView mTime; if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
private TextView mCallName; mCheckBox.setVisibility(View.VISIBLE); ///设置可见行为可见
private NoteItemData mItemData; mCheckBox.setChecked(checked); ///格子打钩
private CheckBox mCheckBox; } else {
mCheckBox.setVisibility(View.GONE);
}
public NotesListItem(Context context) {
super(context); mItemData = data;
inflate(context, R.layout.note_item, this); ///设置控件属性一共三种情况由data的id和父id是否与保存到文件夹的id一致来决定
mAlert = (ImageView) findViewById(R.id.iv_alert_icon); if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {
mTitle = (TextView) findViewById(R.id.tv_title); mCallName.setVisibility(View.GONE);
mTime = (TextView) findViewById(R.id.tv_time); mAlert.setVisibility(View.VISIBLE);
mCallName = (TextView) findViewById(R.id.tv_name); //设置该textview的style
mCheckBox = (CheckBox) findViewById(android.R.id.checkbox); mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
} //settext为设置内容
mTitle.setText(context.getString(R.string.call_record_folder_name)
public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) { + context.getString(R.string.format_folder_files_count, data.getNotesCount()));
if (choiceMode && data.getType() == Notes.TYPE_NOTE) { mAlert.setImageResource(R.drawable.call_record);
mCheckBox.setVisibility(View.VISIBLE); } else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) {
mCheckBox.setChecked(checked); mCallName.setVisibility(View.VISIBLE);
} else { mCallName.setText(data.getCallName());
mCheckBox.setVisibility(View.GONE); mTitle.setTextAppearance(context,R.style.TextAppearanceSecondaryItem);
} mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
///关于闹钟的设置
mItemData = data; if (data.hasAlert()) {
mAlert.setImageResource(R.drawable.clock);//图片来源的设置
if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) { mAlert.setVisibility(View.VISIBLE);
mCallName.setVisibility(View.GONE); } else {
mAlert.setVisibility(View.VISIBLE); mAlert.setVisibility(View.GONE);
}
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); } else {
mCallName.setVisibility(View.GONE);
mTitle.setText(context.getString(R.string.call_record_folder_name) mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
+ context.getString(R.string.format_folder_files_count, data.getNotesCount())); ///设置title格式
mAlert.setImageResource(R.drawable.call_record); if (data.getType() == Notes.TYPE_FOLDER) {
} else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) { mTitle.setText(data.getSnippet()
mCallName.setVisibility(View.VISIBLE); + context.getString(R.string.format_folder_files_count,
mCallName.setText(data.getCallName()); data.getNotesCount()));
mTitle.setTextAppearance(context,R.style.TextAppearanceSecondaryItem); mAlert.setVisibility(View.GONE);
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); } else {
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
if (data.hasAlert()) { if (data.hasAlert()) {
mAlert.setImageResource(R.drawable.clock); mAlert.setImageResource(R.drawable.clock);///设置图片来源
mAlert.setVisibility(View.VISIBLE); mAlert.setVisibility(View.VISIBLE);
} else { } else {
mAlert.setVisibility(View.GONE); mAlert.setVisibility(View.GONE);
} }
} else { }
mCallName.setVisibility(View.GONE); }
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); ///设置内容获取相关时间从data里编辑的日期中获取
mTime. setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
if (data.getType() == Notes.TYPE_FOLDER) {
mTitle.setText(data.getSnippet() setBackground(data);
+ context.getString(R.string.format_folder_files_count, }
data.getNotesCount())); //根据data的文件属性来设置背景
mAlert.setVisibility(View.GONE); private void setBackground(NoteItemData data) {
} else { int id = data.getBgColorId();
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet())); //若是note型文件则4种情况对于4种不同情况的背景来源
if (data.hasAlert()) { if (data.getType() == Notes.TYPE_NOTE) {
mAlert.setImageResource(R.drawable.clock); //单个数据并且只有一个子文件夹
mAlert.setVisibility(View.VISIBLE); if (data.isSingle() || data.isOneFollowingFolder()) {
} else { setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id));
mAlert.setVisibility(View.GONE); } else if (data.isLast()) {//是最后一个数据
} setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id));
} } else if (data.isFirst() || data.isMultiFollowingFolder()) {//是一个数据并有多个子文件夹
} setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id));
} else {
mTime. setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate())); setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id));
}
setBackground(data); } else {
} //若不是note直接调用文件夹的背景来源
setBackgroundResource(NoteItemBgResources.getFolderBgRes());
private void setBackground(NoteItemData data) { }
int id = data.getBgColorId(); }
public NoteItemData getItemData() {
if (data.getType() == Notes.TYPE_NOTE) { return mItemData;
}
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 {
setBackgroundResource(NoteItemBgResources.getFolderBgRes());
}
}
public NoteItemData getItemData() {
return mItemData;
}
}
Loading…
Cancel
Save