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.content.Intent; 22 import android.graphics.Typeface; 23 import android.os.Bundle; 24 import android.view.Menu; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.ArrayAdapter; 28 import android.widget.ListView; 29 import android.widget.TextView; 30 31 import java.util.ArrayList; 32 import java.util.List; 33 34 public class MyRssReader3 extends Activity{ 35 36 private ArrayList<RssItem> mFeeds; 37 ListView mRssList; 38 ArrayAdapter mAdap; 39 private static final int ADD_ELEMENT_REQUEST = 1; 40 41 @Override onCreate(Bundle savedInstanceState)42 public void onCreate(Bundle savedInstanceState){ 43 super.onCreate(savedInstanceState); 44 45 // Load screen layout. 46 setContentView(R.layout.main_screen2); 47 48 // Populate our list 49 mFeeds = initializeList(); 50 51 // Populate ArrayAdapter and bind it to ListView 52 mRssList = (ListView)findViewById(R.id.rssListView); 53 mAdap = new RssDataAdapter<RssItem>(this, R.layout.list_element, mFeeds); 54 mRssList.setAdapter(mAdap); 55 56 // Set the last selected item. 57 // (icicle is only set if this is being restarted). 58 if(savedInstanceState != null && savedInstanceState.containsKey("lastIndexItem")) 59 mRssList.setSelection(savedInstanceState.getInteger("lastIndexItem")); 60 } 61 62 // Store our state before we are potentially bumped from memory. 63 // We'd like to store the current ListView selection. 64 @Override onSaveInstanceState(Bundle outState)65 protected void onSaveInstanceState(Bundle outState){ 66 int index = mRssList.getSelectedItemIndex(); 67 if(index > -1){ 68 outState.putInteger("lastIndexItem", index); 69 } 70 } 71 72 73 // Add our initial menu options. We will tweak this menu when it's loaded swap out 74 // "start service" or "stop service", depending on whether the service is currently running. 75 @Override onCreateOptionsMenu(Menu menu)76 public boolean onCreateOptionsMenu(Menu menu) 77 { 78 // Always call the superclass implementation to 79 // provide standard items. 80 super.onCreateOptionsMenu(menu); 81 82 menu.add(0, 0, R.string.menu_option_start, null); 83 menu.add(0, 1, R.string.menu_option_stop, null); 84 menu.add(0, 2, R.string.menu_option_add, null); 85 menu.add(0, 3, R.string.menu_option_delete, null); 86 menu.add(0, 4, R.string.menu_option_update, null); 87 88 return true; 89 } 90 91 // Toggle out start service/stop service depending on whether the service is running. 92 @Override onPrepareOptionsMenu(Menu menu)93 public boolean onPrepareOptionsMenu(Menu menu){ 94 return true; 95 } 96 97 // Handle our menu clicks. 98 @Override onOptionsItemSelected(Menu.Item item)99 public boolean onOptionsItemSelected(Menu.Item item){ 100 super.onOptionsItemSelected(item); 101 102 switch (item.getId()){ 103 case 0: // Start service 104 showAlert(null, "You clicked 'start'!", "ok", null, false, null); 105 break; 106 case 1: // Stop service 107 showAlert(null, "You clicked stop!", "ok", null, false, null); 108 break; 109 case 2: // Add Item 110 Intent addIntent = new Intent(AddRssItem.class); 111 112 // Use an ID so that if we create a "remove item" form we 113 // can tell which form is returning a value. 114 startActivityForResult(addIntent, ADD_ELEMENT_REQUEST); 115 break; 116 case 3: // Delete item. 117 if(mRssList.hasFocus()){ 118 Object selectedItem = mRssList.getSelectedItem(); 119 mAdap.removeObject(mRssList.getSelectedItem()); 120 } 121 break; 122 case 4: // Update all 123 showAlert(null, "You clicked 'Update'!", "ok", null, false, null); 124 break; 125 default: 126 showAlert(null, "I have no idea what you clicked!", "ok", null, false, null); 127 break; 128 } 129 return true; 130 } 131 132 // Called by the "Add RSS Item" floating screen when it closes. 133 @Override onActivityResult(int requestCode, int resultCode, Intent data)134 protected void onActivityResult(int requestCode, int resultCode, Intent data){ 135 if(resultCode == RESULT_OK){ 136 switch (requestCode){ 137 case ADD_ELEMENT_REQUEST: 138 RssItem newIt = new RssItem( 139 data.getStringExtra("url").toString(), 140 data.getStringExtra("title").toString()); 141 mAdap.addObject(newIt); 142 mRssList.setSelection(mRssList.getCount() - 1); 143 break; 144 default: 145 break; 146 } 147 } 148 } 149 150 // Our private ArrayAdapter implementation that returns a bold TextView for 151 // RSS items that are unread, or a normal TextView for items that have been read. 152 private class RssDataAdapter<T> extends ArrayAdapter<T> { RssDataAdapter(Context context, int resource, List objects)153 public RssDataAdapter(Context context, int resource, List objects) { 154 super(context, resource, objects); 155 } 156 157 // Here's our only important override--returning the list item. 158 @Override getView(int position, View convertView, ViewGroup parent)159 public View getView(int position, View convertView, ViewGroup parent){ 160 161 // Get the item from the underlying array, 162 // Create a TextView wrapper, and change the typeface, if necessary. 163 RssItem item = (RssItem)this.getItem(position); 164 TextView view = new TextView(parent.getContext()); 165 view.setText(item.toString()); 166 167 if(! item.hasBeenRead){ 168 Typeface type = view.getTypeface(); 169 view.setTypeface(Typeface.create(type, Typeface.BOLD_ITALIC)); 170 } 171 return view; 172 } 173 } 174 175 // Method to initialize our list of RSS items. initializeList()176 private ArrayList<RssItem> initializeList(){ 177 ArrayList<RssItem> list = new ArrayList<RssItem>(); 178 list.add(new RssItem("http://www.sciam.com/xml/sciam.xml", "Scientific American")); 179 list.add(new RssItem("http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml", "BBC")); 180 list.add(new RssItem("http://feeds.theonion.com/theonion/daily", "The Onion")); 181 list.add(new RssItem("http://feeds.engadget.com/weblogsinc/engadget", "Engadget")); 182 return list; 183 } 184 } 185