Update NotesListAdapter.java

main
pf45q8f3g 7 months ago
parent 5e71f05915
commit 87d663816f

@ -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"); * 使 Apache License, Version 2.0
* 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 * 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; package net.micode.notes.ui;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.CursorAdapter; import android.widget.CursorAdapter;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes;
import java.util.Collection;
import java.util.Collection; import java.util.HashMap;
import java.util.HashMap; import java.util.HashSet;
import java.util.HashSet; import java.util.Iterator;
import java.util.Iterator;
/**
* NotesListAdapter 便
/* * CursorAdapter便 ListView
* 便CursorAdaptercursorListView */
* NotesListAdapter便 public class NotesListAdapter extends CursorAdapter {
*/ private static final String TAG = "NotesListAdapter";
public class NotesListAdapter extends CursorAdapter { private Context mContext;
private static final String TAG = "NotesListAdapter"; private HashMap<Integer, Boolean> mSelectedIndex;
private Context mContext; private int mNotesCount; // 便签数
private HashMap<Integer, Boolean> mSelectedIndex; private boolean mChoiceMode; // 选择模式标记
private int mNotesCount; //便签数
private boolean mChoiceMode; //选择模式标记 /**
*
/* */
* widget public static class AppWidgetAttribute {
*/ public int widgetId;
public static class AppWidgetAttribute { public int widgetType;
public int widgetId; }
public int widgetType;
}; /**
* NotesListAdapter
/* * @param context
* 便 */
* public NotesListAdapter(Context context) {
*/ super(context, null); // 父类对象置空
public NotesListAdapter(Context context) { mSelectedIndex = new HashMap<Integer, Boolean>(); // 新建选项下标的哈希表
super(context, null); //父类对象置空 mContext = context;
mSelectedIndex = new HashMap<Integer, Boolean>(); //新建选项下标的hash表 mNotesCount = 0;
mContext = context; }
mNotesCount = 0;
} @Override
/**
@Override *
/* * @param context
* * @param cursor
* 使NotesListItem * @param parent
*/ * @return
public View newView(Context context, Cursor cursor, ViewGroup parent) { */
return new NotesListItem(context); public View newView(Context context, Cursor cursor, ViewGroup parent) {
} return new NotesListItem(context);
}
/*
* @Override
* /**
*/ *
@Override * @param view
public void bindView(View view, Context context, Cursor cursor) { * @param context
if (view instanceof NotesListItem) { * @param cursor
//若view是NotesListItem的一个实例 */
NoteItemData itemData = new NoteItemData(context, cursor); public void bindView(View view, Context context, Cursor cursor) {
((NotesListItem) view).bind(context, itemData, mChoiceMode, if (view instanceof NotesListItem) {
isSelectedItem(cursor.getPosition())); NoteItemData itemData = new NoteItemData(context, cursor);
//则新建一个项目选项并且用bind跟将view和鼠标内容便签数据捆绑在一起 ((NotesListItem) view).bind(context, itemData, mChoiceMode,
} isSelectedItem(cursor.getPosition()));
} }
}
/*
* /**
* *
*/ * @param position
public void setCheckedItem(final int position, final boolean checked) { * @param checked
mSelectedIndex.put(position, checked); */
//根据定位和是否勾选设置下标 public void setCheckedItem(final int position, final boolean checked) {
notifyDataSetChanged(); mSelectedIndex.put(position, checked);
//在修改后刷新activity notifyDataSetChanged();
} }
/* /**
* *
*/ * @return true false
public boolean isInChoiceMode() { */
return mChoiceMode; public boolean isInChoiceMode() {
} return mChoiceMode;
}
/*
* /**
* mode *
*/ * @param mode
public void setChoiceMode(boolean mode) { */
mSelectedIndex.clear(); public void setChoiceMode(boolean mode) {
mChoiceMode = mode; mSelectedIndex.clear();
} mChoiceMode = mode;
}
/*
* /**
* *
*/ * @param checked
public void selectAll(boolean checked) { */
Cursor cursor = getCursor(); public void selectAll(boolean checked) {
//获取光标位置 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);
} }
} }
} }
//遍历所有光标可用的位置在判断为便签类型之后勾选单项框 }
}
/**
/* * ID
* * @return ID
* */
*/ 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()) {
//建立hash表 if (mSelectedIndex.get(position) == true) {
for (Integer position : mSelectedIndex.keySet()) { Long id = getItemId(position);
//遍历所有的关键 if (id == Notes.ID_ROOT_FOLDER) {
if (mSelectedIndex.get(position) == true) { Log.d(TAG, "Wrong item id, should not happen");
//若光标位置可用 } else {
Long id = getItemId(position); itemSet.add(id);
if (id == Notes.ID_ROOT_FOLDER) { }
//原文件不需要添加 }
Log.d(TAG, "Wrong item id, should not happen"); }
} else { return itemSet;
itemSet.add(id); }
}
//则将id该下标假如选项集合中 /**
*
} * @return
} */
public HashSet<AppWidgetAttribute> getSelectedWidget() {
return itemSet; HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
} for (Integer position : mSelectedIndex.keySet()) {
if (mSelectedIndex.get(position) == true) {
/* Cursor c = (Cursor) getItem(position);
* Widget if (c != null) {
* AppWidgetAttribute widget = new AppWidgetAttribute();
*/ NoteItemData item = new NoteItemData(mContext, c);
public HashSet<AppWidgetAttribute> getSelectedWidget() { widget.widgetId = item.getWidgetId();
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>(); widget.widgetType = item.getWidgetType();
for (Integer position : mSelectedIndex.keySet()) { itemSet.add(widget);
if (mSelectedIndex.get(position) == true) { } else {
Cursor c = (Cursor) getItem(position); Log.e(TAG, "Invalid cursor");
//以上4句和getSelectedItemIds一样不再重复 return null;
if (c != null) { }
//光标位置可用的话就建立新的Widget属性并编辑下标和类型最后添加到选项集中 }
AppWidgetAttribute widget = new AppWidgetAttribute(); }
NoteItemData item = new NoteItemData(mContext, c); return itemSet;
widget.widgetId = item.getWidgetId(); }
widget.widgetType = item.getWidgetType();
itemSet.add(widget); /**
/** *
* Don't close cursor here, only the adapter could close it * @return
*/ */
} else { public int getSelectedCount() {
Log.e(TAG, "Invalid cursor"); Collection<Boolean> values = mSelectedIndex.values();
return null; if (null == values) {
} return 0;
} }
} Iterator<Boolean> iter = values.iterator();
return itemSet; int count = 0;
} while (iter.hasNext()) {
if (true == iter.next()) {
/* count++;
* }
* }
*/ return count;
public int getSelectedCount() { }
Collection<Boolean> values = mSelectedIndex.values();
//首先获取选项下标的值 /**
if (null == values) { *
return 0; * @return true false
} */
Iterator<Boolean> iter = values.iterator(); public boolean isAllSelected() {
//初始化叠加器 int checkedCount = getSelectedCount();
int count = 0; return (checkedCount != 0 && checkedCount == mNotesCount);
while (iter.hasNext()) { }
if (true == iter.next()) {
//若value值为真计数+1 /**
count++; *
} * @param position
} * @return true false
return count; */
} public boolean isSelectedItem(final int position) {
if (null == mSelectedIndex.get(position)) {
/* return false;
* }
* return mSelectedIndex.get(position);
*/ }
public boolean isAllSelected() {
int checkedCount = getSelectedCount(); @Override
return (checkedCount != 0 && checkedCount == mNotesCount); /**
//获取选项数看是否等于便签的个数 * 便
} */
protected void onContentChanged() {
/* super.onContentChanged();
* calcNotesCount();
* }
*/
public boolean isSelectedItem(final int position) { @Override
if (null == mSelectedIndex.get(position)) { /**
return false; * 便
} * @param cursor
return mSelectedIndex.get(position); */
} public void changeCursor(Cursor cursor) {
super.changeCursor(cursor);
@Override calcNotesCount();
/* }
* activity便
* /**
*/ * 便
protected void onContentChanged() { */
super.onContentChanged(); private void calcNotesCount() {
//执行基类函数 mNotesCount = 0;
calcNotesCount(); for (int i = 0; i < getCount(); i++) {
} Cursor c = (Cursor) getItem(i);
if (c != null) {
@Override if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {
/* mNotesCount++;
* activity便 }
*/ } else {
public void changeCursor(Cursor cursor) { Log.e(TAG, "Invalid cursor");
super.changeCursor(cursor); return;
//执行基类函数 }
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;
}
//否则报错
}
}
}
Loading…
Cancel
Save