|
|
|
@ -29,52 +29,62 @@ 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
p// 声明一个名为FoldersListAdapter的公共类,它继承自CursorAdapter。CursorAdapter是Android中用于绑定数据到ListView的适配器类。
|
|
|
|
|
public class FoldersListAdapter extends CursorAdapter {
|
|
|
|
|
|
|
|
|
|
// 定义一个静态字符串数组PROJECTION,其中包含两个元素,分别是NoteColumns.ID和NoteColumns.SNIPPET。这通常用于数据库查询时指定要检索的列。
|
|
|
|
|
public static final String [] PROJECTION = {
|
|
|
|
|
NoteColumns.ID,
|
|
|
|
|
NoteColumns.SNIPPET
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 定义两个静态整数ID_COLUMN和NAME_COLUMN,分别表示上述PROJECTION数组中的列索引。
|
|
|
|
|
public static final int ID_COLUMN = 0;
|
|
|
|
|
public static final int NAME_COLUMN = 1;
|
|
|
|
|
|
|
|
|
|
// 构造函数,接收Context(应用程序环境)、Cursor(数据库查询结果集)作为参数。调用父类构造函数初始化CursorAdapter。
|
|
|
|
|
public FoldersListAdapter(Context context, Cursor c) {
|
|
|
|
|
super(context, c);
|
|
|
|
|
// TODO Auto-generated constructor stub
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重写newView方法,用于创建新的视图。在这个例子中,新的视图是一个FolderListItem实例。
|
|
|
|
|
@Override
|
|
|
|
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
|
|
|
|
return new FolderListItem(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重写bindView方法,用于将数据绑定到视图上。如果视图是FolderListItem的实例,那么会根据cursor中的数据来设置文件夹名称。
|
|
|
|
|
@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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 公开方法getFolderName,接收一个Context和一个位置参数,返回对应位置的文件夹名称。如果ID列的值等于Notes.ID_ROOT_FOLDER,则返回移动父文件夹的字符串资源;否则返回名称列的值。
|
|
|
|
|
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 {
|
|
|
|
|
private TextView mName; // 定义一个TextView成员变量mName,用于显示文件夹名称。
|
|
|
|
|
|
|
|
|
|
// 构造函数,接收一个Context参数,调用父类构造函数初始化FolderListItem,并inflate布局文件到当前实例。然后通过findViewById方法找到TextView控件mName。
|
|
|
|
|
public FolderListItem(Context context) {
|
|
|
|
|
super(context);
|
|
|
|
|
inflate(context, R.layout.folder_list_item, this); // 加载并初始化布局文件。
|
|
|
|
|
mName = (TextView) findViewById(R.id.tv_folder_name); // 找到并初始化TextView控件mName。
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 公开方法bind,接收一个字符串参数name,用于设置TextView的文本内容。
|
|
|
|
|
public void bind(String name) {
|
|
|
|
|
mName.setText(name); // 将name设置为mName的文本内容。
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|