From bf01f6f4263abd0de955c91ddccdb1279739a0a8 Mon Sep 17 00:00:00 2001 From: pz96pqh8i <3326392379@qq.com> Date: Tue, 31 Dec 2024 02:06:52 +0800 Subject: [PATCH] ADD file via upload --- FoldersListAdapter.java | 164 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 FoldersListAdapter.java diff --git a/FoldersListAdapter.java b/FoldersListAdapter.java new file mode 100644 index 0000000..e4c0d4b --- /dev/null +++ b/FoldersListAdapter.java @@ -0,0 +1,164 @@ +/* + * 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; + + +public class FoldersListAdapter extends CursorAdapter { + 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) { + 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); + 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); + mName = (TextView) findViewById(R.id.tv_folder_name); + } + + public void bind(String name) { + mName.setText(name); + } + } + +} +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)和文件夹名称片段(SNIPPET,可能用于表示文件夹名称等相关信息)两列。 + public static final String [] PROJECTION = { + NoteColumns.ID, + NoteColumns.SNIPPET + }; + + // 定义常量,表示查询结果中ID列对应的索引位置,方便后续从Cursor中获取数据,这里索引为0。 + public static final int ID_COLUMN = 0; + // 定义常量,表示查询结果中用于表示文件夹名称(或名称片段)列对应的索引位置,方便后续从Cursor中获取数据,这里索引为1。 + public static final int NAME_COLUMN = 1; + + // 构造函数,用于初始化FoldersListAdapter实例,接收上下文和数据库游标(Cursor)作为参数,调用父类的构造函数完成基础的初始化工作。 + public FoldersListAdapter(Context context, Cursor c) { + super(context, c); + // TODO Auto-generated constructor stub:此处可能是预留的代码编写位置,目前为空,可根据后续需求添加额外的初始化逻辑。 + } + + // 重写CursorAdapter的newView方法,该方法用于创建一个新的视图(View)对象,作为列表中的一个新的列表项视图。 + // 在这里创建并返回一个自定义的FolderListItem视图对象,用于展示文件夹相关信息。 + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + return new FolderListItem(context); + } + + // 重写CursorAdapter的bindView方法,该方法用于将游标(Cursor)中当前位置的数据绑定到指定的视图(View)上,进行数据展示等操作。 + @Override + public void bindView(View view, Context context, Cursor cursor) { + // 判断传入的视图是否是FolderListItem类型的实例,确保进行正确的绑定操作。 + if (view instanceof FolderListItem) { + // 获取文件夹名称相关信息,根据游标中获取的ID列的值进行判断,如果ID等于Notes.ID_ROOT_FOLDER(可能是代表根文件夹的特定标识), + // 则从资源文件中获取对应的特定字符串(可能是表示“上级文件夹”之类的提示文本)作为文件夹名称;否则从游标中获取对应的名称列(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的bind方法,将获取到的文件夹名称设置到对应的视图中的TextView组件上进行显示。 + ((FolderListItem) view).bind(folderName); + } + } + + // 定义一个方法,用于获取指定位置的文件夹名称,接收上下文和位置索引作为参数。 + // 通过调用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组件,用于显示文件夹的名称信息。 + private TextView mName; + + // 构造函数,用于初始化FolderListItem实例,传入上下文对象,调用父类的构造函数完成基础初始化, + // 并加载对应的布局文件(folder_list_item.xml)到该视图中,然后获取布局中的TextView组件。 + 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); + } + } +} \ No newline at end of file