org.jamienicol.episodes.ShowNotesFragment.java Source code

Java tutorial

Introduction

Here is the source code for org.jamienicol.episodes.ShowNotesFragment.java

Source

/*
 * Copyright (C) 2015 Daniele Ricci <daniele.athome@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jamienicol.episodes;

import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import org.jamienicol.episodes.db.ShowsProvider;
import org.jamienicol.episodes.db.ShowsTable;

public class ShowNotesFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
    private int showId;
    private TextView notesView;

    public static ShowNotesFragment newInstance(int showId) {
        ShowNotesFragment instance = new ShowNotesFragment();

        Bundle args = new Bundle();
        args.putInt("showId", showId);

        instance.setArguments(args);
        return instance;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        showId = getArguments().getInt("showId");
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.show_notes_fragment, container, false);

        notesView = (TextView) view.findViewById(R.id.notes);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        final Bundle loaderArgs = new Bundle();
        loaderArgs.putInt("showId", showId);
        getLoaderManager().initLoader(0, loaderArgs, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        final int showId = args.getInt("showId");
        final Uri uri = Uri.withAppendedPath(ShowsProvider.CONTENT_URI_SHOWS, String.valueOf(showId));
        final String[] projection = { ShowsTable.COLUMN_NOTES };
        return new CursorLoader(getActivity(), uri, projection, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        if (data != null && data.moveToFirst()) {

            final int notesColumnIndex = data.getColumnIndexOrThrow(ShowsTable.COLUMN_NOTES);
            if (data.isNull(notesColumnIndex)) {
                notesView.setText("");
            } else {
                final String text = data.getString(notesColumnIndex);
                notesView.setText(text);
            }
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        onLoadFinished(loader, null);
    }
}