|
|
/*
|
|
|
* 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;
|
|
|
|
|
|
/**
|
|
|
* FoldersListAdapter 是一个用于显示文件夹列表的适配器,它继承自 CursorAdapter。
|
|
|
* 该适配器负责将数据库中的文件夹数据绑定到视图上。
|
|
|
*/
|
|
|
public class FoldersListAdapter extends CursorAdapter {
|
|
|
// 定义查询数据库时需要投影的列,即需要从数据库中获取的列
|
|
|
public static final String [] PROJECTION = {
|
|
|
NoteColumns.ID, // 文件夹的 ID 列
|
|
|
NoteColumns.SNIPPET // 文件夹的名称列
|
|
|
};
|
|
|
|
|
|
// 定义投影列在数组中的索引位置,方便后续使用
|
|
|
public static final int ID_COLUMN = 0; // ID 列的索引
|
|
|
public static final int NAME_COLUMN = 1; // 名称列的索引
|
|
|
|
|
|
/**
|
|
|
* 构造函数,用于创建 FoldersListAdapter 实例。
|
|
|
*
|
|
|
* @param context 上下文对象,用于访问应用资源
|
|
|
* @param c 包含文件夹数据的游标
|
|
|
*/
|
|
|
public FoldersListAdapter(Context context, Cursor c) {
|
|
|
super(context, c);
|
|
|
// TODO Auto-generated constructor stub
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建一个新的视图来显示游标中的数据。
|
|
|
*
|
|
|
* @param context 上下文对象,用于访问应用资源
|
|
|
* @param cursor 包含文件夹数据的游标
|
|
|
* @param parent 新视图的父视图
|
|
|
* @return 返回一个新的 FolderListItem 视图
|
|
|
*/
|
|
|
@Override
|
|
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
|
|
return new FolderListItem(context);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将游标中的数据绑定到指定的视图上。
|
|
|
*
|
|
|
* @param view 要绑定数据的视图
|
|
|
* @param context 上下文对象,用于访问应用资源
|
|
|
* @param cursor 包含文件夹数据的游标
|
|
|
*/
|
|
|
@Override
|
|
|
public void bindView(View view, Context context, Cursor cursor) {
|
|
|
// 检查视图是否为 FolderListItem 类型
|
|
|
if (view instanceof FolderListItem) {
|
|
|
// 获取文件夹名称,如果文件夹的 ID 是根文件夹的 ID,则显示特定的字符串
|
|
|
String folderName = (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
|
|
|
.getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
|
|
|
// 调用 FolderListItem 的 bind 方法将文件夹名称绑定到视图上
|
|
|
((FolderListItem) view).bind(folderName);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据指定的位置获取文件夹的名称。
|
|
|
*
|
|
|
* @param context 上下文对象,用于访问应用资源
|
|
|
* @param position 文件夹在列表中的位置
|
|
|
* @return 返回文件夹的名称
|
|
|
*/
|
|
|
public String getFolderName(Context context, int position) {
|
|
|
// 获取指定位置的游标
|
|
|
Cursor cursor = (Cursor) getItem(position);
|
|
|
// 获取文件夹名称,如果文件夹的 ID 是根文件夹的 ID,则显示特定的字符串
|
|
|
return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
|
|
|
.getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* FolderListItem 是一个自定义的线性布局,用于显示文件夹名称。
|
|
|
*/
|
|
|
private class FolderListItem extends LinearLayout {
|
|
|
private TextView mName; // 用于显示文件夹名称的文本视图
|
|
|
|
|
|
/**
|
|
|
* 构造函数,用于创建 FolderListItem 实例。
|
|
|
*
|
|
|
* @param context 上下文对象,用于访问应用资源
|
|
|
*/
|
|
|
public FolderListItem(Context context) {
|
|
|
super(context);
|
|
|
// 加载文件夹列表项的布局文件
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
} |