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.
xiaomi/ui/FoldersListAdapter.java

101 lines
5.7 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.
*/
// 包声明表明该类所在的包名为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等于根文件夹IDNotes.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);
}
// 自定义的方法用于将传入的文件夹名称设置到对应的TextViewmName控件上进行具体的文本显示设置使得列表项能正确展示文件夹名称信息。
public void bind(String name) {
mName.setText(name);
}
}
}