From 44ab3e7551a1caf639e9000730bd3a21dd3768b1 Mon Sep 17 00:00:00 2001 From: LZ <2929718516@qq.com> Date: Thu, 26 Dec 2024 00:17:22 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=99=E6=AE=B5=E4=BB=A3=E7=A0=81=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E4=BA=86=E4=B8=80=E4=B8=AA=E7=94=A8=E4=BA=8E=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E6=96=87=E4=BB=B6=E5=A4=B9=E5=88=97=E8=A1=A8=E7=9A=84?= =?UTF-8?q?=E9=80=82=E9=85=8D=E5=99=A8FoldersListAdapter=EF=BC=8C=E5=AE=83?= =?UTF-8?q?=E7=BB=A7=E6=89=BF=E8=87=AACursorAdapter=EF=BC=8C=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E5=B0=86=E6=95=B0=E6=8D=AE=E5=BA=93=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9=E6=95=B0=E6=8D=AE=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E5=88=B0UI=E8=A7=86=E5=9B=BE=E4=B8=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FoldersListAdapter.java | 93 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 FoldersListAdapter.java diff --git a/FoldersListAdapter.java b/FoldersListAdapter.java new file mode 100644 index 0000000..1ddec4b --- /dev/null +++ b/FoldersListAdapter.java @@ -0,0 +1,93 @@ +/* + * 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上 + } + } + +} \ No newline at end of file