You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mim/NotesListAdapter.java

233 lines
7.7 KiB

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