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 5: Intents, Broadcast Receivers, Adapters, and the Internet<br />

}<br />

(ListView)this.findViewById(R.id.earthquakeListView);<br />

int layoutID = android.R.layout.simple_list_item_1;<br />

aa = new ArrayAdapter(this, layoutID , earthquakes);<br />

earthquakeListView.setAdapter(aa);<br />

refreshEarthquakes();<br />

The Internet lookup is currently happening on the main UI thread. This is bad form as the application<br />

will become unresponsive if the lookup takes longer than a few seconds. In Chapter 8, you’ll learn how<br />

to move expensive or time-consuming operations like this onto the background thread.<br />

9. If you run your project, you should see a List View that features the earthquakes from the last<br />

24 hours with a magnitude greater than 2.5, as shown in the screenshot in Figure 5-6.<br />

Figure 5-6<br />

10. There are only two more steps to make this a more useful application. First, create a new menu<br />

item to let users refresh the earthquake feed on demand.<br />

10.1. Start by adding a new external string for the menu option.<br />

Refresh Earthquakes<br />

10.2 Then override the Activity’s onCreateOptionsMenu and onOptionsItemSelected<br />

methods to display and handle the refresh earthquakes menu item.<br />

static final private int MENU_UPDATE = Menu.FIRST;<br />

@Override<br />

153

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

Saved successfully!

Ooh no, something went wrong!