com.schedule.MessagesFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.schedule.MessagesFragment.java

Source

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * 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.schedule;

import static com.schedule.Constants.TARGET_INDEX;

import com.schedule.db.DataProviderContract.MessageEntry;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.webkit.WebView;
import android.widget.ListView;
import android.widget.Toast;

/**
 * Fragment that displays a news article.
 */
public class MessagesFragment extends ListFragment {
    // The webview where we display the article (our only view)
    //   WebView mWebView;

    SimpleCursorAdapter mAdapter = null;
    // The article we are to display
    NewsArticle mNewsArticle = null;

    //The cursor for a target's messages
    private Cursor mCursor = null;

    // Parameterless constructor is needed by framework
    public MessagesFragment() {
        super();
    }

    //    /**
    //     * Sets up the UI. It consists if a single WebView.
    //     */
    //    @Override
    //    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //       // mWebView = new WebView(getActivity());
    //        loadWebView();
    //        return mWebView;
    //    }
    //    

    final static String[] projection = new String[] { MessageEntry._ID, MessageEntry.COLUMN_CONTENT };
    final static String selection = MessageEntry.COLUMN_TARGET_ID + "=?";

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onViewCreated(view, savedInstanceState);

        int mTargetIndex = 1;

        if (getArguments() != null) {
            mTargetIndex = getArguments().getInt(TARGET_INDEX, 0);
        }

        String[] selectionArgs = new String[] { String.valueOf(mTargetIndex) };

        mCursor = getActivity().managedQuery(MessageEntry.MESSAGE_TABLE_CONTENTURI, projection, selection,
                selectionArgs, null);

        String[] from = new String[] { MessageEntry.COLUMN_CONTENT };
        int[] to = new int[] { R.id.tv_content };

        mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.message_list_item, mCursor, from, to);
        setListAdapter(mAdapter);
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        getListView().setLayoutAnimation(
                AnimationUtils.loadLayoutAnimation(getActivity(), R.anim.layout_bottom_to_top_slide));
    }

    /**
      * Displays a particular article.
      *
      * @param article the article to display
      */
    public void displayMessage(long targetId) {

        String[] selectionArgs = new String[] { String.valueOf(targetId) };
        //     Toast.makeText(getActivity(), "id:"+targetId+",selectArgs:"+selectionArgs.toString(), 1).show();
        mCursor = getActivity().managedQuery(MessageEntry.MESSAGE_TABLE_CONTENTURI, projection, selection,
                selectionArgs, null);

        mAdapter.changeCursor(mCursor);

        getListView().setLayoutAnimation(
                AnimationUtils.loadLayoutAnimation(getActivity(), R.anim.layout_bottom_to_top_slide));
        // mAdapter.notifyDataSetChanged();
    }

    //    /**
    //     * Loads article data into the webview.
    //     *
    //     * This method is called internally to update the webview's contents to the appropriate
    //     * article's text.
    //     */
    //    void loadWebView() {
    //        if (mWebView != null) {
    //            mWebView.loadData(mNewsArticle == null ? "" : mNewsArticle.getBody(), "text/html",
    //                        "utf-8");
    //        }
    //    }
}