Home | History | Annotate | Download | only in rssexample
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.codelab.rssexample;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.graphics.Typeface;
     22 import android.os.Bundle;
     23 import android.view.Menu;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.widget.ArrayAdapter;
     27 import android.widget.ListView;
     28 import android.widget.TextView;
     29 
     30 import java.util.ArrayList;
     31 import java.util.List;
     32 import java.util.logging.Logger;
     33 public class MyRssReader2 extends Activity{
     34     private ArrayList<RssItem> mFeeds = null;
     35     ListView mRssList = null;
     36     private Logger mLogger = Logger.getLogger("com.example.codelab.rssexample");
     37 
     38     @Override
     39     public void onCreate(Bundle savedInstanceState){
     40         super.onCreate(savedInstanceState);
     41 
     42         // Load screen layout.
     43         setContentView(R.layout.main_screen2);
     44 
     45        // Populate our list
     46         mFeeds = initializeList();
     47         mLogger.info("MyRssReader.onCreate-1  mFeeds value:" + mFeeds.size());
     48 // BEGIN_INCLUDE(2_2)
     49         // Populate ArrayAdapter and bind it to ListView
     50         mRssList = (ListView)findViewById(R.id.rssListView);
     51         if(mRssList == null){
     52             // Note: Calling showAlert() would fail here because dialogs opened
     53             // in onCreate won't be displayed properly, if at all.
     54             mLogger.warning("MyRssReader.onCreate-2 -- Couldn't instantiate a ListView!");
     55         }
     56         RssDataAdapter<RssItem> adap = new RssDataAdapter<RssItem>(this, R.layout.add_item, mFeeds);
     57         if(adap == null){
     58             mLogger.warning("MyRssReader.onCreate-3 -- Couldn't instantiate RssDataAdapter!");
     59         }
     60         if(mFeeds == null){
     61             mLogger.warning("MyRssReader.onCreate-4 -- Couldn't instantiate a ListView!");
     62         }
     63         mRssList.setAdapter(adap);
     64 // END_INCLUDE(2_2)
     65 
     66         mLogger.info("MyRssReader.onCreate-5 -- Loading preferences.");
     67         // Set the last selected item.
     68         // (icicle is only set if this is being restarted).
     69         if(savedInstanceState != null && savedInstanceState.containsKey("lastIndexItem"))
     70         {
     71             Integer selectedItem = savedInstanceState.getInteger("lastIndexItem");
     72             if(selectedItem >= 0 && selectedItem < mRssList.getChildCount()){
     73                 mRssList.setSelection(savedInstanceState.getInteger("lastIndexItem"));
     74             }
     75             mLogger.info("MyRssReader.onCreate-6 -- Last selected item:" + selectedItem);
     76         }
     77     }
     78 
     79     // Store our state before we are potentially bumped from memory.
     80     // We'd like to store the current ListView selection.
     81     @Override
     82     protected void onSaveInstanceState(Bundle outState){
     83         int index = mRssList.getSelectedItemIndex();
     84         if(index > -1){
     85             outState.putInteger("lastIndexItem", index);
     86         }
     87     }
     88 
     89 
     90 
     91     // Add our initial menu options. We will tweak this menu when it's loaded swap out
     92     // "start service" or "stop service", depending on whether the service is currently running.
     93     @Override
     94     public boolean onCreateOptionsMenu(Menu menu){
     95         // Always call the superclass implementation to
     96         // provide standard items.
     97         super.onCreateOptionsMenu(menu);
     98 
     99         menu.add(0, 0, "Start RSS Service", null);
    100         menu.add(0, 1, "Stop RSS Service", null);
    101         menu.add(0, 2, "Add New Feed", null);
    102         menu.add(0, 3, "Delete Feed", null);
    103         menu.add(0, 4, "Update All Feeds", null);
    104 
    105         return true;
    106     }
    107 
    108     // Toggle out start service/stop service depending on whether the service is running.
    109     @Override
    110     public boolean onPrepareOptionsMenu(Menu menu){
    111         return true;
    112     }
    113 
    114     // Handle our menu clicks.
    115     @Override
    116     public boolean onOptionsItemSelected(Menu.Item item){
    117         switch (item.getId()){
    118             case 0:
    119               showAlert(null, "You clicked 'start'!", "ok", null, false, null);
    120               break;
    121             case 1:
    122               showAlert(null, "You clicked stop!", "ok", null, false, null);
    123               break;
    124             case 2:
    125                 showAlert(null, "You clicked 'Add'!", "ok", null, false, null);
    126                 break;
    127             case 3:
    128                 showAlert(null, "You clicked 'Delete'!", "ok", null, false, null);
    129                 break;
    130             case 4:
    131                 showAlert(null, "You clicked 'Update'!", "ok", null, false, null);
    132                 break;
    133             default:
    134                 showAlert(null, "I have no idea what you clicked!", "ok", null, false, null);
    135                 break;
    136             }
    137         return true;
    138     }
    139 
    140 
    141     // Our private ArrayAdapter implementation that returns a bold TextView for
    142     // RSS items that are unread, or a normal TextView for items that have been read.
    143 // BEGIN_INCLUDE(2_3)
    144     private class RssDataAdapter<T> extends ArrayAdapter<T> {
    145         public RssDataAdapter(Context context, int resource, List objects) {
    146             super(context, resource, objects);
    147         }
    148 // END_INCLUDE(2_3)
    149         // Here's our only important override--returning the list item.
    150         @Override
    151         public View getView(int position, View convertView, ViewGroup parent){
    152             TextView view = null;
    153             // Get the item from the underlying array,
    154             // Create a TextView wrapper, and change the typeface, if necessary.
    155             RssItem item = (RssItem)this.getItem(position);
    156             if(item != null)
    157             {
    158                 view = new TextView(parent.getContext());
    159                 view.setText(item.toString());
    160 
    161                 if(! item.hasBeenRead){
    162                     Typeface type = view.getTypeface();
    163                     view.setTypeface(Typeface.create(type, Typeface.BOLD_ITALIC));
    164                 }
    165             }
    166             return view;
    167         }
    168      }
    169 
    170 //BEGIN_INCLUDE(2_1)
    171     // Method to initialize our list of RSS items.
    172     private ArrayList<RssItem> initializeList(){
    173         ArrayList<RssItem> list = new ArrayList<RssItem>();
    174         list.add(new RssItem("http://www.sciam.com/xml/sciam.xml", "Scientific American"));
    175         list.add(new RssItem("http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml", "BBC"));
    176         list.add(new RssItem("http://www.theonion.com/content/feeds/daily.", "The Onion"));
    177         list.add(new RssItem("http://feeds.engadget.com/weblogsinc/engadget", "Engadget"));
    178         return list;
    179     }
    180 //END_INCLUDE(2_1)
    181 }
    182