/* * 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; // 定义一个继承自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; // 构造函数,初始化上下文和游标 public FoldersListAdapter(Context context, Cursor c) { super(context, c); // TODO Auto-generated constructor stub } // 新建视图的方法,返回一个FolderListItem对象 @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return new FolderListItem(context); } // 绑定视图和数据的方法,将游标中的数据设置到视图上 @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); } } // 根据位置获取文件夹名称的方法 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,继承自LinearLayout private class FolderListItem extends LinearLayout { // 定义一个TextView用于显示文件夹名称 private TextView mName; // FolderListItem的构造函数,初始化布局和TextView public FolderListItem(Context context) { super(context); inflate(context, R.layout.folder_list_item, this); mName = (TextView) findViewById(R.id.tv_folder_name); } // 绑定文件夹名称到TextView的方法 public void bind(String name) { mName.setText(name); } } }