注释说明了适配器的用途和基本功能

pull/30/head
gq 2 months ago
parent 5c66d9ef96
commit 861aaddbcf

@ -29,52 +29,103 @@ import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
/**
*
* CursorAdapterCursor
*/
public class FoldersListAdapter extends CursorAdapter {
public static final String [] PROJECTION = {
NoteColumns.ID,
NoteColumns.SNIPPET
// 查询列名数组定义需要从Cursor中获取的列
public static final String[] PROJECTION = {
NoteColumns.ID, // 文件夹ID列
NoteColumns.SNIPPET // 文件夹名称/片段列
};
public static final int ID_COLUMN = 0;
public static final int NAME_COLUMN = 1;
// 列索引常量
public static final int ID_COLUMN = 0; // ID列的索引位置
public static final int NAME_COLUMN = 1; // 名称列的索引位置
/**
*
* @param context
* @param c Cursor
*/
public FoldersListAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
// TODO: 可在此处添加初始化代码
}
/**
*
* @param context
* @param cursor Cursor
* @param parent
* @return
*/
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// 创建新的FolderListItem实例
return new FolderListItem(context);
}
/**
*
* @param view
* @param context
* @param cursor 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);
// 根据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);
}
}
/**
*
* @param context
* @param position
* @return
*/
public String getFolderName(Context context, int position) {
// 获取指定位置的Cursor对象
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);
// 根据ID判断是否是根文件夹返回对应的名称
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;
private TextView mName; // 显示文件夹名称的TextView
/**
*
* @param context
*/
public FolderListItem(Context context) {
super(context);
// 从布局文件填充视图
inflate(context, R.layout.folder_list_item, this);
// 查找名称TextView
mName = (TextView) findViewById(R.id.tv_folder_name);
}
/**
*
* @param name
*/
public void bind(String name) {
// 设置TextView文本
mName.setText(name);
}
}
}

Loading…
Cancel
Save