/* * 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; // 指定每个列表项的布局模板,指定Cursor中的每个字段应该绑定到那些视图上 // 以文件夹的形式展现给用户 public class FoldersListAdapter extends CursorAdapter { // 指定查询时返回的列,NoteColumns是一个接口 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 } // 创建一个新视图,并初始化子标签 @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return new FolderListItem(context); } // 用于绑定视图和数据 @Override public void bindView(View view, Context context, Cursor cursor) { // 判断视图对象是否为 FolderListItem 的实例 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); // 根据文件夹id获取文件夹的名称 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); // 将获取到的TextView对象赋值给mName mName = (TextView) findViewById(R.id.tv_folder_name); } // 将给定的名称设置到mName中 public void bind(String name) { mName.setText(name); } } }