注释代码7

pq5n3hobs 8 months ago
parent 9327ff9a1c
commit ef66289336

@ -0,0 +1,102 @@
/*
* 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;
// FoldersListAdapter类继承自CursorAdapterCursorAdapter是Android中用于将数据库游标Cursor中的数据与ListView等可展示列表的视图组件进行绑定的适配器类
// 这个类的主要作用是将数据库中关于文件夹相关的数据适配展示到对应的列表视图中,方便用户查看文件夹相关信息
public class FoldersListAdapter extends CursorAdapter {
// 定义一个字符串数组用于指定从数据库查询时需要获取的列名这里只选取了文件夹的IDNoteColumns.ID和片段信息NoteColumns.SNIPPET两列数据
// 具体的列名定义应该在Notes.NoteColumns类中有相应的常量声明后续查询数据库时会依据此数组获取对应的数据
public static final String [] PROJECTION = {
NoteColumns.ID,
NoteColumns.SNIPPET
};
// 定义一个常量表示在查询结果游标Cursor中文件夹ID列对应的索引位置初始化为0方便后续通过游标获取对应列的数据
public static final int ID_COLUMN = 0;
// 定义一个常量表示在查询结果游标Cursor中用于展示名称相关列这里根据代码逻辑推测可能是从SNIPPET列获取类似文件夹名称的信息对应的索引位置初始化为1
public static final int NAME_COLUMN = 1;
// 构造方法调用父类CursorAdapter的构造方法传入上下文对象Context和数据库游标Cursor用于初始化适配器使其与特定的数据源游标数据关联起来
public FoldersListAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub这里是自动生成代码时的占位注释实际开发中如果有需要可以在这里添加更多初始化相关的逻辑
}
// 重写父类的newView方法该方法用于创建一个新的视图View对象作为列表中的每一项显示的容器
// 在这里返回一个自定义的FolderListItem实例意味着每个列表项的视图结构将由FolderListItem类来定义和初始化
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return new FolderListItem(context);
}
// 重写父类的bindView方法该方法用于将游标Cursor中的数据绑定到已经创建好的视图View对象上实现数据的展示
// 也就是将从数据库获取到的文件夹相关数据填充到对应的列表项视图中,使其正确显示相应的文件夹信息
@Override
public void bindView(View view, Context context, Cursor cursor) {
if (view instanceof FolderListItem) {
// 根据游标中获取的文件夹ID判断是否为根文件夹通过与Notes.ID_ROOT_FOLDER比较
// 如果是根文件夹则获取对应的字符串资源R.string.menu_move_parent_folder通常是一个固定的用于表示根文件夹的文本比如"根文件夹"之类的提示语)作为文件夹名称
// 如果不是根文件夹则从游标中获取对应列NAME_COLUMN所指向的列即前面定义的NoteColumns.SNIPPET列推测用于存储文件夹名称相关信息的字符串数据作为文件夹名称
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);
}
}
// 自定义的方法用于根据给定的位置position获取对应位置上文件夹的名称信息
// 通过调用getItem方法获取对应位置的游标对象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);
}
// 定义一个内部私有类FolderListItem继承自LinearLayout意味着它可以作为一个线性布局容器来承载其他子视图组件用于构建列表项的具体视图结构
private class FolderListItem extends LinearLayout {
// 定义一个TextView组件用于在列表项中显示文件夹的名称信息后续通过操作这个TextView来设置具体的文件夹名称文本内容
private TextView mName;
// 构造方法调用父类LinearLayout的构造方法传入上下文对象Context用于初始化这个线性布局容器
// 然后通过inflate方法加载指定的布局资源文件R.layout.folder_list_item到当前的线性布局中使其具备相应的视图结构
// 最后通过findViewById方法获取布局文件中定义的用于显示文件夹名称的TextView组件实例
public FolderListItem(Context context) {
super(context);
inflate(context, R.layout.folder_list_item, this);
mName = (TextView) findViewById(R.id.tv_folder_name);
}
// 定义一个方法用于将传入的文件夹名称name设置到前面获取的TextView组件mName实现文件夹名称在列表项视图中的显示
public void bind(String name) {
mName.setText(name);
}
}
}
Loading…
Cancel
Save