|
|
/*
|
|
|
* 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.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
import android.widget.CursorAdapter;
|
|
|
import android.widget.LinearLayout;
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
import net.micode.notes.data.Notes;
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
|
/**
|
|
|
* 文件夹列表适配器,用于小米便签中展示文件夹列表(如移动便签时选择目标文件夹)
|
|
|
* 继承自CursorAdapter,负责将数据库中的文件夹数据与列表视图绑定
|
|
|
*/
|
|
|
public class FoldersListAdapter extends CursorAdapter {
|
|
|
/**
|
|
|
* 数据库查询投影:指定需要查询的文件夹字段
|
|
|
* 包含文件夹ID(用于区分不同文件夹)和文件夹名称(存储在SNIPPET字段)
|
|
|
*/
|
|
|
public static final String [] PROJECTION = {
|
|
|
NoteColumns.ID, // 文件夹ID
|
|
|
NoteColumns.SNIPPET // 文件夹名称(小米便签中文件夹名称存储在此字段)
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 投影字段对应的索引,用于从Cursor中快速获取数据
|
|
|
*/
|
|
|
public static final int ID_COLUMN = 0; // PROJECTION中ID字段的索引
|
|
|
public static final int NAME_COLUMN = 1; // PROJECTION中SNIPPET(名称)字段的索引
|
|
|
|
|
|
/**
|
|
|
* 构造方法:初始化文件夹列表适配器
|
|
|
* @param context 上下文环境(如调用文件夹列表的Activity或对话框)
|
|
|
* @param c 包含文件夹数据的数据库游标(查询结果)
|
|
|
*/
|
|
|
public FoldersListAdapter(Context context, Cursor c) {
|
|
|
super(context, c);
|
|
|
// TODO Auto-generated constructor stub
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建新的列表项视图
|
|
|
* @param context 上下文环境
|
|
|
* @param cursor 包含当前项数据的游标(此处未直接使用,仅用于创建视图)
|
|
|
* @param parent 父视图容器(通常为ListView)
|
|
|
* @return 自定义的文件夹列表项视图(FolderListItem实例)
|
|
|
*/
|
|
|
@Override
|
|
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
|
|
return new FolderListItem(context);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将游标中的文件夹数据绑定到列表项视图
|
|
|
* @param view 列表项视图(FolderListItem实例)
|
|
|
* @param context 上下文环境
|
|
|
* @param cursor 包含当前文件夹数据的游标
|
|
|
*/
|
|
|
@Override
|
|
|
public void bindView(View view, Context context, Cursor cursor) {
|
|
|
if (view instanceof FolderListItem) {
|
|
|
// 处理根文件夹的特殊名称:根文件夹ID为Notes.ID_ROOT_FOLDER,显示为“移动到父文件夹”
|
|
|
String folderName = (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
|
|
|
.getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
|
|
|
// 将文件夹名称绑定到列表项视图
|
|
|
((FolderListItem) view).bind(folderName);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据位置获取文件夹名称
|
|
|
* @param context 上下文环境
|
|
|
* @param position 列表项位置
|
|
|
* @return 对应位置的文件夹名称(根文件夹特殊处理)
|
|
|
*/
|
|
|
public String getFolderName(Context context, int position) {
|
|
|
Cursor cursor = (Cursor) getItem(position);
|
|
|
// 根文件夹特殊处理,其他文件夹直接取SNIPPET字段值
|
|
|
return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
|
|
|
.getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 自定义文件夹列表项视图,继承LinearLayout,用于展示单个文件夹的名称
|
|
|
*/
|
|
|
private class FolderListItem extends LinearLayout {
|
|
|
private TextView mName; // 用于显示文件夹名称的文本控件
|
|
|
|
|
|
/**
|
|
|
* 构造方法:初始化列表项布局和控件
|
|
|
* @param context 上下文环境
|
|
|
*/
|
|
|
public FolderListItem(Context context) {
|
|
|
super(context);
|
|
|
// 加载文件夹列表项的布局文件(folder_list_item.xml)
|
|
|
inflate(context, R.layout.folder_list_item, this);
|
|
|
// 获取布局中的文本控件,用于显示文件夹名称
|
|
|
mName = (TextView) findViewById(R.id.tv_folder_name);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 为列表项设置文件夹名称
|
|
|
* @param name 文件夹名称(可能是根文件夹的特殊名称或普通文件夹名称)
|
|
|
*/
|
|
|
public void bind(String name) {
|
|
|
mName.setText(name);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |