diff --git a/src/Notes-master/app/src/main/java/net/micode/notes/ui/FoldersListAdapter.java b/src/Notes-master/app/src/main/java/net/micode/notes/ui/FoldersListAdapter.java index 96b77da..06153a8 100644 --- a/src/Notes-master/app/src/main/java/net/micode/notes/ui/FoldersListAdapter.java +++ b/src/Notes-master/app/src/main/java/net/micode/notes/ui/FoldersListAdapter.java @@ -1,17 +1,9 @@ /* * 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. + * 版权声明:本代码受Apache许可证2.0版本保护 + * 您可以在遵守许可证的前提下使用、修改和分发本代码 + * 许可证全文可在http://www.apache.org/licenses/LICENSE-2.0获取 */ package net.micode.notes.ui; @@ -28,53 +20,103 @@ import net.micode.notes.R; import net.micode.notes.data.Notes; import net.micode.notes.data.Notes.NoteColumns; - +/** + * 文件夹列表适配器 + * 功能:将数据库中的文件夹数据绑定到ListView/RecyclerView中 + * 特点:支持自定义文件夹名称显示,特别是对根文件夹的特殊处理 + */ public class FoldersListAdapter extends CursorAdapter { + // 数据库查询投影:指定要查询的列 public static final String [] PROJECTION = { - NoteColumns.ID, - NoteColumns.SNIPPET + NoteColumns.ID, // 文件夹ID + NoteColumns.SNIPPET // 文件夹名称(片段) }; - public static final int ID_COLUMN = 0; - public static final int NAME_COLUMN = 1; + // 列索引常量,方便代码引用 + public static final int ID_COLUMN = 0; // ID列索引 + public static final int NAME_COLUMN = 1; // 名称列索引 + /** + * 构造函数 + * @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) { 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); + // 获取文件夹名称,特殊处理根文件夹 + 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); - return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context - .getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN); + // 同样特殊处理根文件夹 + return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? + context.getString(R.string.menu_move_parent_folder) : + cursor.getString(NAME_COLUMN); } + /** + * 文件夹列表项内部类 + * 功能:定义单个文件夹项的UI布局和数据绑定逻辑 + */ private class FolderListItem extends LinearLayout { + // 文件夹名称显示文本视图 private TextView mName; + /** + * 构造函数 + * @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); } } - -} +} \ No newline at end of file