/* * 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. */ // 包声明,表明该类所在的包名为net.micode.notes.ui 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)中的数据适配到ListView等可展示列表视图组件上的适配器类, // 在这里主要用于处理文件夹相关数据的展示,比如将文件夹的名称等信息展示在列表中。 public class FoldersListAdapter extends CursorAdapter { // 定义查询数据库时使用的投影(即要查询的列),用于获取文件夹相关信息,这里包含文件夹的ID和摘要(可能用于显示文件夹名称等相关用途)两列。 public static final String [] PROJECTION = { NoteColumns.ID, NoteColumns.SNIPPET }; // 定义列索引常量,方便后续从游标(Cursor)中获取对应列的数据,这里分别对应ID列和名称相关列(通过SNIPPET列来获取文件夹名称等情况)的索引。 public static final int ID_COLUMN = 0; public static final int NAME_COLUMN = 1; // 构造函数,用于创建FoldersListAdapter实例,接收上下文(Context)和数据库游标(Cursor)作为参数, // 调用父类(CursorAdapter)的构造函数进行初始化,将传入的游标与适配器关联起来,以便后续处理数据展示相关操作。 public FoldersListAdapter(Context context, Cursor c) { super(context, c); // TODO Auto-generated constructor stub } // 重写CursorAdapter的抽象方法newView,该方法的作用是创建一个新的视图(View)用于展示游标中的每一项数据, // 在这里返回一个自定义的FolderListItem实例,它继承自LinearLayout,用于展示文件夹相关信息的具体布局。 @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return new FolderListItem(context); } // 重写CursorAdapter的抽象方法bindView,该方法用于将游标中当前位置的数据绑定到指定的视图(View)上,进行具体的数据展示设置, // 比如设置文本内容到对应的TextView等控件中,在这里根据游标中的数据判断文件夹名称的显示内容,并调用FolderListItem的bind方法进行展示设置。 @Override public void bindView(View view, Context context, Cursor cursor) { if (view instanceof FolderListItem) { // 判断如果当前游标中获取的文件夹ID等于根文件夹ID(Notes.ID_ROOT_FOLDER,具体含义由Notes类定义), // 则显示特定的表示根文件夹的字符串(从资源文件中获取对应的文本),否则显示游标中对应列(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); } } // 自定义的方法,用于获取指定位置的文件夹名称,通过传入上下文(Context)和列表中的位置(position)参数, // 根据位置获取对应的游标(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); } // 内部私有类,继承自LinearLayout,用于定义展示文件夹信息的具体布局和相关操作逻辑,它是每个列表项的具体视图实现。 private class FolderListItem extends LinearLayout { // 用于显示文件夹名称的TextView控件,通过在构造函数中查找布局文件中的对应控件实例进行后续操作。 private TextView mName; // 构造函数,接收上下文(Context)参数,调用父类(LinearLayout)的构造函数进行初始化, // 并通过inflate方法将指定的布局文件(R.layout.folder_list_item)填充到当前视图中,然后获取布局中的TextView控件实例。 public FolderListItem(Context context) { super(context); inflate(context, R.layout.folder_list_item, this); mName = (TextView) findViewById(R.id.tv_folder_name); } // 自定义的方法,用于将传入的文件夹名称设置到对应的TextView(mName)控件上,进行具体的文本显示设置,使得列表项能正确展示文件夹名称信息。 public void bind(String name) { mName.setText(name); } } }