Update FoldersListAdapter.java

main
Sunique_L 2 years ago
parent af946123e6
commit c9d0b5b212

@ -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的文本内容。
}
}
}
Loading…
Cancel
Save