|
|
/*
|
|
|
* 该类实现了一个自定义的文件夹列表适配器,用于在Android应用中展示文件夹列表的数据,并绑定到相应的视图上。
|
|
|
*/
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
// 创建新的视图
|
|
|
@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);
|
|
|
}
|
|
|
}
|
|
|
}
|