com.lillicoder.newsblurry.stories.StoryFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.lillicoder.newsblurry.stories.StoryFragment.java

Source

/**
 * Copyright 2012 Scott Weeden-Moody
 *
 * 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 com.lillicoder.newsblurry.stories;

import junit.framework.Assert;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;

import com.lillicoder.newsblurry.R;

public class StoryFragment extends Fragment {

    private TextView _title;
    private TextView _byline;
    private WebView _storyContent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_story, null);

        this._title = (TextView) root.findViewById(R.id.StoryFragment_title);
        this._byline = (TextView) root.findViewById(R.id.StoryFragment_byline);
        this._storyContent = (WebView) root.findViewById(R.id.StoryFragment_storyContent);

        return root;
    }

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

        Intent intent = this.getActivity().getIntent();
        if (intent != null && intent.hasExtra(StoryActivity.INTENT_STORY_TO_DISPLAY)) {
            Story story = (Story) intent.getSerializableExtra(StoryActivity.INTENT_STORY_TO_DISPLAY);
            this.displayStory(story);
        }
    }

    /**
     * Displays the given {@link Story} on this fragment.
     * @param story {@link Story} to display.
     */
    private void displayStory(Story story) {
        Assert.assertTrue(story != null);

        this._title.setText(story.getTitle());

        // TODO set author line
        //this._byline.setText(story.getByline());

        this._storyContent.loadData(story.getContent(), "text/html", "utf-8");
    }

}