You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
5MI/FoldersListAdapter.java

93 lines
4.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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.
*/
// 定义一个名为FoldersListAdapter的类该类继承自CursorAdapter
package net.micode.notes.ui;
import android.content.Context; // 导入Context类用于访问应用环境信息
import android.database.Cursor; // 导入Cursor类用于数据库查询结果集的遍历
import android.view.View; // 导入View类用于UI界面上的元素
import android.view.ViewGroup; // 导入ViewGroup类用于容纳其他视图元素
import android.widget.CursorAdapter; // 导入CursorAdapter类用于将Cursor数据绑定到视图上
import android.widget.LinearLayout; // 导入LinearLayout类用于创建线性布局的视图
import android.widget.TextView; // 导入TextView类用于显示文本
import net.micode.notes.R; // 导入R类用于访问应用资源
import net.micode.notes.data.Notes; // 导入Notes类用于访问笔记数据
import net.micode.notes.data.Notes.NoteColumns; // 导入NoteColumns内部类用于访问笔记表的列名
// FoldersListAdapter类的定义开始
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的构造函数
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); // 创建一个新的FolderListItem视图
}
// 当需要将数据绑定到视图上时,调用此方法
@Override
public void bindView(View view, Context context, Cursor cursor) {
if (view instanceof FolderListItem) { // 如果视图是FolderListItem的实例
// 根据ID判断是否是根文件夹并获取文件夹名
String folderName = (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
.getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
// 调用FolderListItem的bind方法将文件夹名设置到视图上
((FolderListItem) view).bind(folderName);
}
}
// 根据位置获取文件夹名的方法
public String getFolderName(Context context, int position) {
Cursor cursor = (Cursor) getItem(position); // 获取指定位置的Cursor
// 根据ID判断是否是根文件夹并返回文件夹名
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 {
private TextView mName; // 用于显示文件夹名的TextView
// FolderListItem的构造函数
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); // 将文件夹名设置到TextView上
}
}
}