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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5: Intents, Broadcast Receivers, Adapters, and the Internet<br />

}<br />

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

earthquakeListView.setOnItemClickListener(new OnItemClickListener() {<br />

public void onItemClick(AdapterView _av, View _v, int _index,<br />

long arg3) {<br />

selectedQuake = earthquakes.get(_index);<br />

showDialog(QUAKE_DIALOG);<br />

}<br />

});<br />

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

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

earthquakeListView.setAdapter(aa);<br />

refreshEarthquakes();<br />

11.3. Now override the onCreateDialog and onPrepareDialog methods to create and<br />

populate the Earthquake Details dialog.<br />

@Override<br />

public Dialog onCreateDialog(int id) {<br />

switch(id) {<br />

case (QUAKE_DIALOG) :<br />

LayoutInflater li = LayoutInflater.from(this);<br />

View quakeDetailsView = li.inflate(R.layout.quake_details, null);<br />

}<br />

AlertDialog.Builder quakeDialog = new AlertDialog.Builder(this);<br />

quakeDialog.setTitle(“Quake Time”);<br />

quakeDialog.setView(quakeDetailsView);<br />

return quakeDialog.create();<br />

}<br />

return null;<br />

@Override<br />

public void onPrepareDialog(int id, Dialog dialog) {<br />

switch(id) {<br />

case (QUAKE_DIALOG) :<br />

SimpleDateFormat sdf;<br />

sdf = new SimpleDateFormat(“dd/MM/yyyy HH:mm:ss”);<br />

String dateString = sdf.format(selectedQuake.getDate());<br />

String quakeText = “Mangitude “ + selectedQuake.getMagnitude() +<br />

“\n” + selectedQuake.getDetails() + “\n” +<br />

selectedQuake.getLink();<br />

}<br />

}<br />

AlertDialog quakeDialog = (AlertDialog)dialog;<br />

quakeDialog.setTitle(dateString);<br />

TextView tv =<br />

(TextView)quakeDialog.findViewById(R.id.quakeDetailsTextView);<br />

tv.setText(quakeText);<br />

break;<br />

155

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

Saved successfully!

Ooh no, something went wrong!