14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6: Data Storage, Retrieval, and Sharing<br />

168<br />

4. Create the Preferences Activity. It will be used to display the application preferences.<br />

Override onCreate to infl ate the layout you created in Step 2, and get references to the<br />

Checkbox and both Spinner controls. Then make a call to the populateSpinners stub.<br />

package com.paad.earthquake;<br />

import android.app.Activity;<br />

import android.content.SharedPreferences;<br />

import android.content.SharedPreferences.Editor;<br />

import android.os.Bundle;<br />

import android.view.View;<br />

import android.widget.ArrayAdapter;<br />

import android.widget.Button;<br />

import android.widget.CheckBox;<br />

import android.widget.Spinner;<br />

public class Preferences extends Activity {<br />

}<br />

CheckBox autoUpdate;<br />

Spinner updateFreqSpinner;<br />

Spinner magnitudeSpinner;<br />

@Override<br />

public void onCreate(Bundle icicle) {<br />

super.onCreate(icicle);<br />

setContentView(R.layout.preferences);<br />

}<br />

updateFreqSpinner =<br />

(Spinner)findViewById(R.id.spinner_update_freq);<br />

magnitudeSpinner = (Spinner)findViewById(R.id.spinner_quake_mag);<br />

autoUpdate = (CheckBox)findViewById(R.id.checkbox_auto_update);<br />

populateSpinners();<br />

private void populateSpinners() {<br />

}<br />

5. Fill in the populateSpinners method, using Array Adapters to bind each Spinner to its corresponding<br />

array.<br />

private void populateSpinners() {<br />

// Populate the update frequency spinner<br />

ArrayAdapter fAdapter;<br />

fAdapter = ArrayAdapter.createFromResource(this,<br />

R.array.update_freq_options,<br />

android.R.layout.simple_spinner_item);<br />

fAdapter.setDropDownViewResource(<br />

android.R.layout.simple_spinner_dropdown_item);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!