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.
xiaomi-note/week-01/ui/NotesListAdapter.java

195 lines
6.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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 net.micode.notes.tool.SlideHelper;
import net.micode.notes.tool.onContentClickListener;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
//链接前端后端,关联数据库
public class NotesListAdapter extends CursorAdapter {
public static final SlideHelper slideHelper = new SlideHelper();
private static final String TAG = "NotesListAdapter";
private Context mContext;
private HashMap<Integer, Boolean> mSelectedIndex;
private int mNotesCount;
private boolean mChoiceMode;
//挂件包括挂件的id与类型
private onContentClickListener mContentClick;
//便签列表适配器
public NotesListAdapter(Context context, onContentClickListener contentClick) {
super(context, null);
mSelectedIndex = new HashMap<Integer, Boolean>();
mContext = context;
mNotesCount = 0;
mContentClick = contentClick;
}
//创建新的视图,参数为链接,游标(光标的类型),视图组件
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return new NotesListItem(mContext);
}
//点击事件
@Override
public void bindView(View view, Context context, Cursor cursor) {
if (view instanceof NotesListItem) {
NoteItemData itemData = new NoteItemData(context, cursor);
((NotesListItem) view).bind(context, itemData, mChoiceMode,
isSelectedItem(cursor.getPosition()));
/*添加内容区域点击事件*/
((NotesListItem) view).setOnContentClick(mContentClick, cursor.getPosition());
}
}
//部件布局
public void setCheckedItem(final int position, final boolean checked) {
mSelectedIndex.put(position, checked);
notifyDataSetChanged();
}
//是否进行移动
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);
}
}
}
}
//批量搜寻数据id
public HashSet<Long> getSelectedItemIds() {
HashSet<Long> itemSet = new HashSet<Long>();
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;
}
//设置搜索数据部件
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);
if (c != null) {
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;
}
//搜索数据个数记录
public int getSelectedCount() {
Collection<Boolean> values = mSelectedIndex.values();//收集boolean类型的数据
if (null == values) {
return 0;
}
Iterator<Boolean> iter = values.iterator();
int count = 0;
while (iter.hasNext()) {
if (true == iter.next()) {
count++;
}
}
return count;//技术
}
//是否搜索到指定位置项目
public boolean isAllSelected() {
int checkedCount = getSelectedCount();
return (checkedCount != 0 && checkedCount == mNotesCount);
}
//判断是否所搜完毕
public boolean isSelectedItem(final int position) {
if (null == mSelectedIndex.get(position)) {
return false;
}
return mSelectedIndex.get(position);
}
//关系发生变换
@Override
protected void onContentChanged() {
super.onContentChanged();
calcNotesCount();
}
//改变游标
@Override
public void changeCursor(Cursor cursor) {
super.changeCursor(cursor);
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;
}
}
}
public static class AppWidgetAttribute {
public int widgetId;
public int widgetType;
}
}