/*
 * 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 {
    //CursorAdapter是Cursor和ListView的接口
    //FoldersListAdapter继承CursorAdapter的类
    //实现让便签数据库与用户交互，这里使用folder(文件夹）形式展现给用户
    public static final String [] PROJECTION = {
        NoteColumns.ID,
        NoteColumns.SNIPPET
    };//调用数据库里的便签ID和片段

    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);
        //到这个文件去，文件操作
    }
    //根据数据库里的标记的ID得出标签的各项内容

    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);
            //根据ID寻找文件信息
        }

        public void bind(String name) {
            mName.setText(name);
        }
        //绑定文件，设置标题
    }
    //在FolderListAdapter类里面声明了一个私有类FolderListItem,继承了LinearLayout以便使用textview控件.
    //该类主要设置了文件夹列表项的标题名称

}
