Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
ActivityInstrumentation / src / com.example.android.activityinstrumentation /

MainActivity.java

       
        1
       
       
        /*
       
       
        2
       
       
        * Copyright 2013 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.android.activityinstrumentation;
       
       
        18
       
       
       
       
        19
       
       
        import android.app.Activity;
       
       
        20
       
       
        import android.content.SharedPreferences;
       
       
        21
       
       
        import android.os.Bundle;
       
       
        22
       
       
        import android.preference.PreferenceManager;
       
       
        23
       
       
        import android.view.View;
       
       
        24
       
       
        import android.widget.AdapterView;
       
       
        25
       
       
        import android.widget.ArrayAdapter;
       
       
        26
       
       
        import android.widget.Spinner;
       
       
        27
       
       
       
       
        28
       
       
        import java.util.ArrayList;
       
       
        29
       
       
        import java.util.Arrays;
       
       
        30
       
       
       
       
        31
       
       
        /**
       
       
        32
       
       
        * Basic activity with a spinner. The spinner should persist its position to disk every time a
       
       
        33
       
       
        * new selection is made.
       
       
        34
       
       
        */
       
       
        35
       
       
        public class MainActivity extends Activity {
       
       
        36
       
       
       
       
        37
       
       
        /** Shared preferences key: Holds spinner position. Must not be negative. */
       
       
        38
       
       
        private static final String PREF_SPINNER_POS = "spinner_pos";
       
       
        39
       
       
        /** Magic constant to indicate that no value is stored for PREF_SPINNER_POS. */
       
       
        40
       
       
        private static final int PREF_SPINNER_VALUE_ISNULL = -1;
       
       
        41
       
       
        /** Values for display in spinner. */
       
       
        42
       
       
        private static final String[] SPINNER_VALUES = new String[] {
       
       
        43
       
       
        "Select Weather...", "Sunny", "Partly Cloudy", "Cloudy", "Rain", "Snow", "Hurricane"};
       
       
        44
       
       
       
       
        45
       
       
        // Constants representing each of the options in SPINNER_VALUES. Declared package-private
       
       
        46
       
       
        // so that they can be accessed from our test suite.
       
       
        47
       
       
        static final int WEATHER_NOSELECTION = 0;
       
       
        48
       
       
        static final int WEATHER_SUNNY = 1;
       
       
        49
       
       
        static final int WEATHER_PARTLY_CLOUDY = 2;
       
       
        50
       
       
        static final int WEATHER_CLOUDY = 3;
       
       
        51
       
       
        static final int WEATHER_RAIN = 4;
       
       
        52
       
       
        static final int WEATHER_SNOW = 5;
       
       
        53
       
       
        static final int WEATHER_HURRICANE = 6;
       
       
        54
       
       
       
       
        55
       
       
        /** Handle to default shared preferences for this activity. */
       
       
        56
       
       
        private SharedPreferences mPrefs;
       
       
        57
       
       
        /** Handle to the spinner in this Activity's layout. */
       
       
        58
       
       
        private Spinner mSpinner;
       
       
        59
       
       
       
       
        60
       
       
        /**
       
       
        61
       
       
        * Setup activity state.
       
       
        62
       
       
        *
       
       
        63
       
       
        * @param savedInstanceState
       
       
        64
       
       
        */
       
       
        65
       
       
        @Override
       
       
        66
       
       
        protected void onCreate(Bundle savedInstanceState) {
       
       
        67
       
       
        super.onCreate(savedInstanceState);
       
       
        68
       
       
       
       
        69
       
       
        // Inflate UI from res/layout/activity_main.xml
       
       
        70
       
       
        setContentView(R.layout.sample_main);
       
       
        71
       
       
       
       
        72
       
       
        // Get handle to default shared preferences for this activity
       
       
        73
       
       
        mPrefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
       
       
        74
       
       
       
       
        75
       
       
        // Populate spinner with sample values from an array
       
       
        76
       
       
        mSpinner = (Spinner) findViewById(R.id.spinner);
       
       
        77
       
       
        mSpinner.setAdapter(
       
       
        78
       
       
        new ArrayAdapter<String>(
       
       
        79
       
       
        this,                                                   // Context
       
       
        80
       
       
        android.R.layout.simple_list_item_1,                    // Layout
       
       
        81
       
       
        new ArrayList<String>(Arrays.asList(SPINNER_VALUES))    // Data source
       
       
        82
       
       
        ));
       
       
        83
       
       
       
       
        84
       
       
        // Read in a sample value, if it's not set.
       
       
        85
       
       
        int selection = mPrefs.getInt(PREF_SPINNER_POS, PREF_SPINNER_VALUE_ISNULL);
       
       
        86
       
       
        if (selection != PREF_SPINNER_VALUE_ISNULL) {
       
       
        87
       
       
        mSpinner.setSelection(selection);
       
       
        88
       
       
        }
       
       
        89
       
       
       
       
        90
       
       
        // Callback to persist spinner data whenever a new value is selected. This will be the
       
       
        91
       
       
        // focus of our sample unit test.
       
       
        92
       
       
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
       
       
        93
       
       
       
       
        94
       
       
        // The methods below commit the ID of the currently selected item in the spinner
       
       
        95
       
       
        // to disk, using a SharedPreferences file.
       
       
        96
       
       
        //
       
       
        97
       
       
        // Note: A common mistake here is to forget to call .commit(). Try removing this
       
       
        98
       
       
        // statement and running the tests to watch them fail.
       
       
        99
       
       
        @Override
       
       
        100
       
       
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
       
       
        101
       
       
        mPrefs.edit().putInt(PREF_SPINNER_POS, position).commit();
       
       
        102
       
       
        }
       
       
        103
       
       
       
       
        104
       
       
        @Override
       
       
        105
       
       
        public void onNothingSelected(AdapterView<?> parent) {
       
       
        106
       
       
        mPrefs.edit().remove(PREF_SPINNER_POS).commit();
       
       
        107
       
       
        }
       
       
        108
       
       
        });
       
       
        109
       
       
        }
       
       
        110
       
       
        }