pull/4/head
fxk 2 years ago
parent c08f360b32
commit 99076f229e

@ -31,56 +31,72 @@ import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
//便签列表适配器
//实现鼠标和编辑便签链接的桥梁
public class NotesListAdapter extends CursorAdapter { public class NotesListAdapter extends CursorAdapter {
private static final String TAG = "NotesListAdapter"; private static final String TAG = "NotesListAdapter";
private Context mContext; private Context mContext;
private HashMap<Integer, Boolean> mSelectedIndex; private HashMap<Integer, Boolean> mSelectedIndex; //所选索引
private int mNotesCount; private int mNotesCount; //便签数量
private boolean mChoiceMode; private boolean mChoiceMode; //选择模式
public static class AppWidgetAttribute { public static class AppWidgetAttribute {
public int widgetId; public int widgetId;
public int widgetType; public int widgetType;
}; };
//初始化NotesListAdapter
public NotesListAdapter(Context context) { public NotesListAdapter(Context context) {
super(context, null); super(context, null); //置空父类对象
mSelectedIndex = new HashMap<Integer, Boolean>(); mSelectedIndex = new HashMap<Integer, Boolean>(); //更新哈希表
mContext = context; mContext = context;
mNotesCount = 0; mNotesCount = 0;
} }
@Override @Override
//新建视图来保存光标指向内容
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
//绑定视图 将视图于光标进行捆绑
public void bindView(View view, Context context, Cursor cursor) { public void bindView(View view, Context context, Cursor cursor) {
if (view instanceof NotesListItem) { if (view instanceof NotesListItem) {
//检查视图是否为NotesListItem类型
//是的话则创建一个新的 NoteItemData 对象并使用它来绑定视图
NoteItemData itemData = new NoteItemData(context, cursor); NoteItemData itemData = new NoteItemData(context, cursor);
((NotesListItem) view).bind(context, itemData, mChoiceMode, ((NotesListItem) view).bind(context, itemData, mChoiceMode,
isSelectedItem(cursor.getPosition())); isSelectedItem(cursor.getPosition()));
} }
} }
//设置勾选框
public void setCheckedItem(final int position, final boolean checked) { public void setCheckedItem(final int position, final boolean checked) {
//根据positionchecked来设置下标
mSelectedIndex.put(position, checked); mSelectedIndex.put(position, checked);
//刷新activity
notifyDataSetChanged(); notifyDataSetChanged();
} }
//判断是否处于选择模式
public boolean isInChoiceMode() { public boolean isInChoiceMode() {
return mChoiceMode; return mChoiceMode;
} }
//设置选择模式
public void setChoiceMode(boolean mode) { public void setChoiceMode(boolean mode) {
mSelectedIndex.clear(); mSelectedIndex.clear();
mChoiceMode = mode; mChoiceMode = mode;
} }
//选择全部选项
public void selectAll(boolean checked) { public void selectAll(boolean checked) {
//获取光标位置
Cursor cursor = getCursor(); Cursor cursor = getCursor();
for (int i = 0; i < getCount(); i++) { for (int i = 0; i < getCount(); i++) {
//遍历所有光标可用的位置在判断为便签类型之后勾选单项框
if (cursor.moveToPosition(i)) { if (cursor.moveToPosition(i)) {
if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) {
setCheckedItem(i, checked); setCheckedItem(i, checked);
@ -89,14 +105,15 @@ public class NotesListAdapter extends CursorAdapter {
} }
} }
//建立选择项的下标列表
public HashSet<Long> getSelectedItemIds() { public HashSet<Long> getSelectedItemIds() {
HashSet<Long> itemSet = new HashSet<Long>(); HashSet<Long> itemSet = new HashSet<Long>();//哈希表
for (Integer position : mSelectedIndex.keySet()) { for (Integer position : mSelectedIndex.keySet()) {//遍历 mSelectedIndex 变量中的所有位置
if (mSelectedIndex.get(position) == true) { if (mSelectedIndex.get(position) == true) {//如果光标位置为true
Long id = getItemId(position); Long id = getItemId(position);
if (id == Notes.ID_ROOT_FOLDER) { if (id == Notes.ID_ROOT_FOLDER) {
Log.d(TAG, "Wrong item id, should not happen"); Log.d(TAG, "Wrong item id, should not happen");
} else { } else {//否则添加到itemSet中
itemSet.add(id); itemSet.add(id);
} }
} }
@ -105,19 +122,23 @@ public class NotesListAdapter extends CursorAdapter {
return itemSet; return itemSet;
} }
//建立桌面Widget的选项表
public HashSet<AppWidgetAttribute> getSelectedWidget() { public HashSet<AppWidgetAttribute> getSelectedWidget() {
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>(); HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
for (Integer position : mSelectedIndex.keySet()) { for (Integer position : mSelectedIndex.keySet()) {
if (mSelectedIndex.get(position) == true) { if (mSelectedIndex.get(position) == true) {
Cursor c = (Cursor) getItem(position); Cursor c = (Cursor) getItem(position);
if (c != null) { if (c != null) {
//光标非空则建立widget并编辑下标和类型
AppWidgetAttribute widget = new AppWidgetAttribute(); AppWidgetAttribute widget = new AppWidgetAttribute();
NoteItemData item = new NoteItemData(mContext, c); NoteItemData item = new NoteItemData(mContext, c);
widget.widgetId = item.getWidgetId(); widget.widgetId = item.getWidgetId();
widget.widgetType = item.getWidgetType(); widget.widgetType = item.getWidgetType();
//添加到itemSet
itemSet.add(widget); itemSet.add(widget);
/** /**
* Don't close cursor here, only the adapter could close it * Don't close cursor here, only the adapter could close it
*
*/ */
} else { } else {
Log.e(TAG, "Invalid cursor"); Log.e(TAG, "Invalid cursor");
@ -128,11 +149,14 @@ public class NotesListAdapter extends CursorAdapter {
return itemSet; return itemSet;
} }
//获取选项个数
public int getSelectedCount() { public int getSelectedCount() {
//获取下标值
Collection<Boolean> values = mSelectedIndex.values(); Collection<Boolean> values = mSelectedIndex.values();
if (null == values) { if (null == values) { //values为空返回0
return 0; return 0;
} }
//初始化
Iterator<Boolean> iter = values.iterator(); Iterator<Boolean> iter = values.iterator();
int count = 0; int count = 0;
while (iter.hasNext()) { while (iter.hasNext()) {
@ -143,12 +167,16 @@ public class NotesListAdapter extends CursorAdapter {
return count; return count;
} }
//判断是否被全部选中
public boolean isAllSelected() { public boolean isAllSelected() {
int checkedCount = getSelectedCount(); int checkedCount = getSelectedCount();
//返回比较是否检查数等于便签数且不为0
return (checkedCount != 0 && checkedCount == mNotesCount); return (checkedCount != 0 && checkedCount == mNotesCount);
} }
//判断是否为选项表
public boolean isSelectedItem(final int position) { public boolean isSelectedItem(final int position) {
//获取的光标位置是否为空
if (null == mSelectedIndex.get(position)) { if (null == mSelectedIndex.get(position)) {
return false; return false;
} }
@ -156,17 +184,20 @@ public class NotesListAdapter extends CursorAdapter {
} }
@Override @Override
//在适配器内容发生更改时调用计算便签数量
protected void onContentChanged() { protected void onContentChanged() {
super.onContentChanged(); super.onContentChanged();
calcNotesCount(); calcNotesCount();
} }
@Override @Override
//光标更改时计算便签数量
public void changeCursor(Cursor cursor) { public void changeCursor(Cursor cursor) {
super.changeCursor(cursor); super.changeCursor(cursor);
calcNotesCount(); calcNotesCount();
} }
//计算便签数量
private void calcNotesCount() { private void calcNotesCount() {
mNotesCount = 0; mNotesCount = 0;
for (int i = 0; i < getCount(); i++) { for (int i = 0; i < getCount(); i++) {

Loading…
Cancel
Save