/*这段代码定义了一个名为 FoldersListAdapter 的类,它是 CursorAdapter 的子类, 主要用于适配数据库查询结果(通过 Cursor 对象表示),并将数据绑定到列表项视图上。 此适配器特别适用于展示文件夹列表,每个文件夹项包含一个名称。*/ /* * 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; /*导入了应用相关的资源和数据模型类*/ public class FoldersListAdapter extends CursorAdapter { /*定义了一个名为 FoldersListAdapter 的公共类,继承自 CursorAdapter*/ public static final String [] PROJECTION = { NoteColumns.ID, NoteColumns.SNIPPET }; /*定义了数据库查询的列名数组*/ public static final int ID_COLUMN = 0; public static final int NAME_COLUMN = 1; /*定义了查询结果中每列的索引*/ public FoldersListAdapter(Context context, Cursor c) { super(context, c); /*构造函数,初始化父类 CursorAdapter*/ // TODO Auto-generated constructor stub } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return new FolderListItem(context); } /*覆盖 newView 方法,返回一个新的视图实例,这里返回的是一个 FolderListItem 实例*/ @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); } } /*覆盖 bindView 方法,将数据绑定到视图上。如果是根目录,则使用特定字符串,否则从 Cursor 中获取名称*/ 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); } /*提供了一个方法用于根据位置获取文件夹名称*/ private class FolderListItem extends LinearLayout { private TextView mName; public FolderListItem(Context context) { super(context); inflate(context, R.layout.folder_list_item, this); mName = (TextView) findViewById(R.id.tv_folder_name); } public void bind(String name) { mName.setText(name); } } /*定义了一个内部类 FolderListItem,它是一个 LinearLayout 的子类,用于表示一个文件夹列表项,并提供了绑定数据的方法*/ }