/* * 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,用于显示文件夹列表。 * 该适配器根据数据库中的 cursor 数据来显示文件夹的名称,并且可以显示特定的根文件夹名称。 */ 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 Cursor 数据源 */ public FoldersListAdapter(Context context, Cursor c) { super(context, c); // TODO Auto-generated constructor stub } /** * 创建一个新的视图用于显示文件夹信息。 * 这个方法会在 ListView 需要新的项时调用。 * @param context 上下文对象 * @param 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 当前游标 */ @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 上 ((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); } /** * 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); mName = (TextView) findViewById(R.id.tv_folder_name); // 获取文件夹名称的 TextView } /** * 将文件夹名称绑定到视图的 TextView 上。 * @param name 文件夹名称 */ public void bind(String name) { mName.setText(name); // 设置文件夹名称 } } }