• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.os.Bundle;
21 import android.view.Menu;
22 import android.widget.ArrayAdapter;
23 import android.widget.ListView;
24 
25 //BEGIN_INCLUDE(1_1)
26 public class MyRssReader extends Activity {
27     /** Called with the activity is first created. */
28     @Override
onCreate(Bundle savedInstanceState)29     public void onCreate(Bundle savedInstanceState){
30         super.onCreate(savedInstanceState);
31 
32         // Load screen layout.
33         setContentView(R.layout.main_screen);
34 
35 //END_INCLUDE(1_1)
36 //BEGIN_INCLUDE(1_2)
37         // Load some simple values into the ListView
38         mRssList = (ListView) findViewById(R.id.rssListView);
39         mRssList.setAdapter(
40                 new ArrayAdapter<String>(
41                         this,
42                         R.layout.list_element,
43                         new String[] { "Scientific American", "BBC", "The Onion", "Engadget" }));
44 //END_INCLUDE(1_2)
45 }
46 
47     // Store our state before we are potentially bumped from memory.
48     // We'd like to store the current ListView selection.
49     @Override
onSaveInstanceState(Bundle outState)50     protected void onSaveInstanceState(Bundle outState){
51         int index = mRssList.getSelectedItemIndex();
52         if(index > -1){
53             outState.putInteger("lastIndexItem", index);
54         }
55     }
56 
57     // Add our initial menu options. We will tweak this menu when it's loaded swap out
58     // "start service" or "stop service", depending on whether the service is currently running.
59     @Override
onCreateOptionsMenu(Menu menu)60     public boolean onCreateOptionsMenu(Menu menu){
61         // Always call the superclass implementation to
62         // provide standard items.
63         super.onCreateOptionsMenu(menu);
64 
65         menu.add(0, 0, "Start RSS Service", null);
66         menu.add(0, 1, "Stop RSS Service", null);
67         menu.add(0, 2, "Add New Feed", null);
68         menu.add(0, 3, "Delete Feed", null);
69         menu.add(0, 4, "Update All Feeds", null);
70 
71         return true;
72     }
73 
74     // Toggle out start service/stop service depending on whether the service is running.
75     @Override
onPrepareOptionsMenu(Menu menu)76     public boolean onPrepareOptionsMenu(Menu menu){
77         return true;
78     }
79 
80     // Handle our menu clicks.
81     @Override
onOptionsItemSelected(Menu.Item item)82     public boolean onOptionsItemSelected(Menu.Item item){
83         switch (item.getId()) {
84             case 0:
85                 showAlert(null, "You clicked 'start'!", "ok", null, false, null);
86                 break;
87             case 1:
88                 showAlert(null, "You clicked stop!", "ok", null, false, null);
89                 break;
90             case 2:
91                 showAlert(null, "You clicked 'Add'!", "ok", null, false, null);
92                 break;
93             case 3:
94                 showAlert(null, "You clicked 'Delete'!", "ok", null, false, null);
95                 break;
96             case 4:
97                 showAlert(null, "You clicked 'Update'!", "ok", null, false, null);
98                 break;
99             default:
100                 showAlert(null, "I have no idea what you clicked!", "ok", null, false, null);
101                 break;
102         }
103         return true;
104     }
105 
106     ListView mRssList;
107 }
108