|
|
/**
|
|
|
* 文件名称:FoldersListAdapter.java
|
|
|
* 版权声明:版权所有 (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
* 许可证声明:根据Apache License, Version 2.0(“许可证”)获得许可;
|
|
|
* 除非遵守许可证,否则不得使用此文件。
|
|
|
* 您可以在以下网址获得许可证的副本:
|
|
|
*
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
*
|
|
|
* 除非适用法律要求或书面同意,否则按“现状”分发的软件是在没有任何明示或暗示的
|
|
|
* 保证和条件的情况下提供的,包括但不限于关于所有权、不侵犯第三方权利、适销性或
|
|
|
* 适用于特定用途的任何保证和条件。有关权限和限制的具体语言,请参见许可证。
|
|
|
*/
|
|
|
|
|
|
package net.micode.notes.ui; // 定义包名
|
|
|
|
|
|
import android.content.Context; // 导入Context类,用于获取系统资源和服务
|
|
|
import android.database.Cursor; // 导入Cursor类,用于访问数据库查询结果
|
|
|
import android.view.View; // 导入View类,用于操作视图
|
|
|
import android.view.ViewGroup; // 导入ViewGroup类,用于操作视图组
|
|
|
import android.widget.CursorAdapter; // 导入CursorAdapter类,用于适配器
|
|
|
import android.widget.LinearLayout; // 导入LinearLayout类,用于线性布局
|
|
|
import android.widget.TextView; // 导入TextView类,用于文本视图
|
|
|
|
|
|
import net.micode.notes.R; // 导入R类,用于访问资源文件
|
|
|
import net.micode.notes.data.Notes; // 导入Notes类,用于访问笔记数据
|
|
|
import net.micode.notes.data.Notes.NoteColumns; // 导入NoteColumns类,用于访问笔记列数据
|
|
|
|
|
|
|
|
|
/**
|
|
|
* FoldersListAdapter 类是一个用于文件夹列表的适配器,继承自CursorAdapter。
|
|
|
*/
|
|
|
public class FoldersListAdapter extends CursorAdapter {
|
|
|
// 定义查询数据库时需要的列
|
|
|
public static final String [] PROJECTION = {
|
|
|
NoteColumns.ID,
|
|
|
NoteColumns.SNIPPET
|
|
|
};
|
|
|
|
|
|
// 定义列索引常量
|
|
|
public static final int ID_COLUMN = 0;
|
|
|
public static final int NAME_COLUMN = 1;
|
|
|
|
|
|
/**
|
|
|
* FoldersListAdapter 构造函数。
|
|
|
* @param context 上下文环境,用于获取系统资源和服务。
|
|
|
* @param c 数据库查询结果的Cursor对象。
|
|
|
*/
|
|
|
public FoldersListAdapter(Context context, Cursor c) {
|
|
|
super(context, c); // 调用父类的构造函数
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建新的视图。
|
|
|
* @param context 上下文环境。
|
|
|
* @param cursor Cursor对象。
|
|
|
* @param parent 父视图组。
|
|
|
* @return 返回新的视图对象。
|
|
|
*/
|
|
|
@Override
|
|
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
|
|
return new FolderListItem(context); // 创建FolderListItem视图
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 绑定视图和数据。
|
|
|
* @param view 视图对象。
|
|
|
* @param context 上下文环境。
|
|
|
* @param cursor Cursor对象。
|
|
|
*/
|
|
|
@Override
|
|
|
public void bindView(View view, Context context, Cursor cursor) {
|
|
|
if (view instanceof FolderListItem) {
|
|
|
// 获取文件夹名称
|
|
|
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); // 获取对应位置的Cursor对象
|
|
|
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; // 用于显示文件夹名称的TextView
|
|
|
|
|
|
/**
|
|
|
* FolderListItem 构造函数。
|
|
|
* @param context 上下文环境。
|
|
|
*/
|
|
|
public FolderListItem(Context context) {
|
|
|
super(context); // 调用父类的构造函数
|
|
|
inflate(context, R.layout.folder_list_item, this); // 从XML布局文件中加载视图
|
|
|
mName = (TextView) findViewById(R.id.tv_folder_name); // 获取TextView对象
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 绑定文件夹名称数据到视图。
|
|
|
* @param name 文件夹名称。
|
|
|
*/
|
|
|
public void bind(String name) {
|
|
|
mName.setText(name); // 设置TextView的文本
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |