Your SlideShare is downloading. ×
0
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Android Programming: Lecture 04
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Android Programming: Lecture 04

2,374

Published on

1) Adapter Design Pattern …

1) Adapter Design Pattern
2) Android Adapters
3) Android Intents
4) Screencasts and source code for this lecture are at

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,374
On Slideshare
0
From Embeds
0
Number of Embeds
31
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Android Programming Lecture 04 Vladimir Kulyukin Department of Computer Science Utah State Universitywww.youtube.com/vkedco www.vkedco.blogspot.com
  • 2. Outline ● Q&A ● Adapter Design Pattern ● Android Adapters – ArrayAdapter – SimpleCursorAdapter ● Android Intentswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 3. Resources Source code and screencasts for this lecture are available at http://vkedco.blogspot.com/2011/10/android-programming-lecture-04.htmlwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 4. Q&A ● Q1: Can I hardcode the shared balanced tree in Assignment 2? ● A1: Yes ● Q2: Do I have to work in a team for the final project? ● A2: No. But good teams tend to produce better projects ● Q3: Can I submit my final project earlier? ● A3: Yeswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 5. Adapter Design Patternwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 6. Adapter Design Pattern ● The intent of the Adapter Design Pattern is to convert the interface of one class into another interface that clients have become used to and expect to have ● Adapters make communication possible among classes with incompatible interfaces ● Adapters provide a common interface to classes and objects with disparate interfaces ● In Software Engineering, adapters are sometimes called wrapperswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 7. Adapter DP Applicability ● Adapter DP is used when: – There is an existing class and its interface does not match the one that is needed – The objective is to create a reusable class that cooperates with unplanned interfaces – There exist several existing subclasses and it is impractical to subclass all of them to adapt their interfaces; in this case, an object adapter can adapt the interface of its parent classwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 8. Adapter DP Implementation: Inheritance Client Target Adaptee Request() SpecificRequest() Adapter Request() SpecificRequest() Target: Defines domain-specific interface Client: Uses target Objects Adaptee: Defines interface that must be adapted to to conform to the Targets interface Adapter: Adapts the interface of Adaptee to Targetwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 9. Adapter DP Implementation: Composition Client Target Adaptee Request() SpecificRequest() Adapter Request() Target: Defines domain-specific interface Adaptee.SpecificRequest() Client: Uses Targets interface Adaptee: Defines interface that must be adapted to conform to Targets interface Adapter: Adapts Adaptees interface to Targets interfacewww.youtube.com/vkedco www.vkedco.blogspot.com
  • 10. Adaptors in C++ STL float x[5] = {1.0, 2.0, 3.0, 4.0, 5.0}; vector<float> v1(&x[0], &x[5]); // reverse iterators are iterator adaptors that adapt regular // iterators float rsum = accumulate(v1.rbegin(), v1.rend(), (float)0.0);www.youtube.com/vkedco www.vkedco.blogspot.com
  • 11. Android Adapterswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 12. Android Adapters ● Android adapters provide a common interface to the data model behind containers ● More specifically, Android adapters generate child views for specific containers from data ● Android adapters are used with container controls that extend android.widget.AdapterView: ListView, GridView, Spinner, Gallerywww.youtube.com/vkedco www.vkedco.blogspot.com
  • 13. Android Adapters ● Two most commonly used Android adapters are ArrayAdapter and SimpleCursorAdapter ● ArrayAdapter is used with list controls and uses TextView to represent child views (list items) ● SimpleCursorAdapter is used with SQLite databases and content providers: converts cursor rows to child views in container controls ● It is possible to create a custom adapters by extending existing adapter classeswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 14. ArrayAdaptorwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 15. ArrayAdapter: Typical Use String[] ary = { “string_0”, “string_1”, “string_2” }; ArrayAdapter<String> adptr = new ArrayAdapter<String>(this, android.R.layout_sipmle_list_item_1, ary); ● 1st argument is Context (this) ● 2nd argument is a text view resource id ( android.R.layout_sipmle_list_item_1 is a text view in Android SDK) rd ● 3 argument is a sequence of objectswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 16. ListViewArrayAdapter1 Project Demonstrates how to use an ArrayAdapter to connect a ListView (container) with a statically allocated array (data model)www.youtube.com/vkedco www.vkedco.blogspot.com
  • 17. ListViewArrayAdapter1 Project: Highlights // Statically allocated array private static final String[] mRumiWorks = { "Spiritual Couplets", "The Works of Shams of Tabriz", "In It Whats In It", "Seven Sessions", "The Letters" };www.youtube.com/vkedco www.vkedco.blogspot.com
  • 18. ListViewArrayAdapter1 Project: Highlights // 1. Get the list view of the current activity ListView lv = this.getListView(); // 2. Create an adapter ArrayAdapter<String> adptr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mRumiWorks); // 3. Bind the adapter to the list view lv.setAdapter(adptr);www.youtube.com/vkedco www.vkedco.blogspot.com
  • 19. ListViewArrayAdapter2 Project Demonstrates how to 1) use an ArrayAdapter to connect a ListView (container) with a dynamically allocated array (data model) that is populated from an XML resource file one item at a time; 2) create a custom child view XML designwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 20. ListViewArrayAdapter2 Project: Highlights // This is rumi_works.xml that defines each element of the array in XML <?xml version="1.0" encoding="utf-8"?> <resources> <string name="spiritual_couplets">Spiritual Couplets</string> <string name="shams_of_tabriz">The Works of Shams of Tabriz</string> <string name="in_it">In It Whats In It</string> <string name="seven_sessions">Seven Sessions</string> <string name="letters">The Letters</string> </resources>www.youtube.com/vkedco www.vkedco.blogspot.com
  • 21. ListViewArrayAdapter2 Project: Highlights // This is list_item_specs.xml that specifies the appearance of each text view in the // container <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="11dp" android:textSize="15sp"> </TextView>www.youtube.com/vkedco www.vkedco.blogspot.com
  • 22. ListViewArrayAdapter2 Project: Highlights private static String[] mRumiWorks = null; // 1. Get a Resources object of this application Resources res = getResources(); // 2. Allocate the array mRumiWorks = new String[5]; // 3. Use the resources object to fill in the array with XML defined strings. mRumiWorks[0] = res.getString(R.string.spiritual_couplets); mRumiWorks[1] = res.getString(R.string.shams_of_tabriz); mRumiWorks[2] = res.getString(R.string.in_it); mRumiWorks[3] = res.getString(R.string.seven_sessions); mRumiWorks[4] = res.getString(R.string.letters);www.youtube.com/vkedco www.vkedco.blogspot.com
  • 23. ListViewArrayAdapter2 Project: Highlights // 4. Set the list adapter of this ListActivity directly. Note that // we are using a customized design of the each text view in // the list view defined in list_item_specs.xml. this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item_specs, mRumiWorks));www.youtube.com/vkedco www.vkedco.blogspot.com
  • 24. ListViewArrayAdapter3 Project Demonstrates how to use an ArrayAdapter to connect a ListView (container) with an array (data model) that is dynamically constructed by a Resources object from an XML resource that defines the entire arraywww.youtube.com/vkedco www.vkedco.blogspot.com
  • 25. ListViewArrayAdapter3 Project: Highlights // This is rumi_works.xml that defines the entire array <?xml version="1.0" encoding="utf-8"?> <resources> <array name="rumi_works"> <item>Spiritual Couplets</item> <item>The Works of Shams of Tabriz</item> <item>In It Whats In It</item> <item>Seven Sessions</item> <item>The Letters</item> </array> </resources>www.youtube.com/vkedco www.vkedco.blogspot.com
  • 26. ListViewArrayAdapter3 Project: Highlightsprivate static String[] mRumiWorks = null;// 1. Get a Resources objectResources res = getResources();// 2. Inflate an array from an XML resource in res/values/rumi_works.xmlmRumiWorks = res.getStringArray(R.array.rumi_works);// 3. Construct and set an ArrayAdaptrsetListAdapter(new ArrayAdapter<String>(this, R.layout.list_item_specs, mRumiWorks));
  • 27. ListViewArrayAdapter3 Project: Highlights // 5. Set the OnItemClickListener of the list view. this.getListView().setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View child, int position, long id) { String msg = ""; msg += "PARENT = " + parent.toString() + "n"; msg += "VIEW = " + child.toString() + "n"; msg += "VIEWS TEXT = " + ((TextView)child).getText().toString() + "n"; msg += "POSITION = " + Integer.toString(position) + "n"; msg += "ID = " + Long.toString(id); // 6. This is a subtle point: we cannot specify this as the first argument to makeText, because we are inside // the scope of OnItemClickListener, not inside the ListActivity where we were in ListViewArrayAdapter1Act. // Thus, we have to getApplicationContext() explicitly. Toast t = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG); t.show(); }});www.youtube.com/vkedco www.vkedco.blogspot.com
  • 28. ListViewArrayAdapter3 Project: Highlights private static String[] mRumiWorks = null; // 1. Get a Resources object Resources res = getResources(); // 2. Inflate an array from an XML resource in res/values/rumi_works.xml mRumiWorks = res.getStringArray(R.array.rumi_works); // 3. Construct and set an ArrayAdaptr setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item_specs, mRumiWorks));www.youtube.com/vkedco www.vkedco.blogspot.com
  • 29. ListViewArrayAdapter4 Project Demonstrates how to construct an ArrayAdapter from an XML string arraywww.youtube.com/vkedco www.vkedco.blogspot.com
  • 30. ListViewArrayAdapter4 Project: Highlights // This is res/values/rumi_works.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="rumi_works"> <item>Spiritual Couplets</item> <item>The Works of Shams of Tabriz</item> <item>In It Whats In It</item> <item>Seven Sessions</item> <item>The Letters</item> </string-array> </resources>www.youtube.com/vkedco www.vkedco.blogspot.com
  • 31. ListViewArrayAdapter4 Project: Highlights // Construct an ArrayAdapter generlized on CharSequence from an XML // array of strings. Does not compile when ArrayAdapter is generalized on String. ArrayAdapter<CharSequence> adptr = ArrayAdapter.createFromResource(this, R.array.rumi_works, R.layout.list_item_specs); this.setListAdapter(adptr);www.youtube.com/vkedco www.vkedco.blogspot.com
  • 32. SimpleCursorAdapterwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 33. SimpleCursorAdapter ListView Result Set TextView Row 0 Data Row 0 TextView Row 1 SimpleCursorAdapter Data Row 1 TextView Row 2 Data Row 2 TextView Row 3 Data Row 3 TextView Row 4 Data Row 4 ChildViewSpecs SimpleCursorAdapter converts a data row in the Cursor to a child view in the container control. ChildViewSpecs is an XML resource.www.youtube.com/vkedco www.vkedco.blogspot.com
  • 34. SimpleCursorAdapter Constructor ● SimpleCursorAdapter(Context c, int layout, Cursor c, String[] from, int[] to); ● int layout – an XML resource specifying a row layout ● String[] from – an array of column names, because data row in the cursor may have multiple columns ● int[] to – an array of text view specs, because each cell in a given column is converted into a text view ● String[] from and int[] to must be of the same lengthwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 35. ContactPicker Project Demonstrates a use of an SimpleCursorAdapterwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 36. ContactPicker Project: Highlights String[] from = new String[] {People.NAME}; // People.NAME has been depricated int[] to = new int[] { R.id.itemTextView }; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listitemlayout, c, from, to); ListView lv = (ListView)findViewById(R.id.contactListView); lv.setAdapter(adapter);www.youtube.com/vkedco www.vkedco.blogspot.com
  • 37. Intentswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 38. Intents ● Mobile apps typically run in their sandboxes ● Sandboxes are constructed primarily for security reasons: they put constraints on how apps interact with each other and with native components ● Any mobile architecture must address the question of how apps can interact across the sandbox boundaries ● In Android OS, this interaction is done through Intents, Broadcast Receivers, and Networking (Internet)www.youtube.com/vkedco www.vkedco.blogspot.com
  • 39. Purposes of Intents ● Intents are used to broadcast data ● Intents are used to start applications and services ● Intents can be explicit and implicit ● Implicit intents are used in runtime binding – the process of letting Android OS determine at runtime which application components can best service the request specified by intents ● Broadcast intents announce events to the entire system ● Broadcast receivers are used to receive broadcast intentswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 40. Explicit Intents ● Explicit intents specify the activity class that they want Android OS to run Intent intent = new Intent(ThisActivity.this, SomeOtherActivity.class); startActivityForResult(intent, request_code); ● An activity that calls startActivityForResult() typically implements onActivityResult() that uses the same request_code ● Calling finish() on the new activity or pressing the back button closes the activity and removes it from the Activity Stackwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 41. Explicit Intents ● An activity that is launched via startActivityForResult must return a result code ● The result code is returned with setResult(int code) ● Two typical values for the code argument are Activity.RESULT_OK and Activity.RESULT_CANCELED ● Since setResult supports any integer values, custom activity communication protocols can be designed ● setResult also has this method signature: setResult(int code, Intent data) ● This second argument is passed to the calling activity as the third argument of onActivityResult()www.youtube.com/vkedco www.vkedco.blogspot.com
  • 42. Native Android Actions ● You can use intents to launch native android actions ● ACTION_ANSWER – open an activity that handles incoming calls ● ACTION_CALL – open an activity with a phone dialer ● ACTION_PICK – launch an activity to pick an item from a list of items ● ACTION_WEB_SEARCH – launch an activity to perform a web search ● ACTION_VIEW – launch an activity that can view the specified data in a reasonable mannerwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 43. Implicit Intents ● Implicit intents provide a mechanism for anonymous (no explicit activity specification) requests ● An activity asks Android OS to do an action by specifying high-level requirements but w/o knowing which activity or application will actually perform the action ● The requesting action can specify a URI to the data which the performing activity must process or add that data to the implicit intentwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 44. Implicit Intents ● Implicit intents provide a mechanism for anonymous requests ● An implicit intent is launched with startActivity(Intent i) ● Activities and applications use intent filters to tell Android OS which actions they can execute and which data types they can work with ● An activity/application that has an intent filter is called an intent handlerwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 45. Logical Structure of an Intent Filter ● Action – Name of the action being serviced – Name should be a unique string – Best practice is to use the Java package naming convention ● Category – When the action should be serviced ● Data – Data types that the action can work withwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 46. Intent Resolution ● Intent resolution starts when an activity calls startActivity() or startActivityForResult() with some intent i ● Android OS gets the list of all intent filters available from the installed packages ● Intent filters that do not match is action or category are removed from the list ● An action match occurs when is action and the filters action are the same or no action is specified ● A category match occurs when is categories and the filters categories are the same ● A data match occurs when is data types are the same as the filters data typeswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 47. Intent Resolution ● Native android applications and activities are part of the Intent resolution process ● Native apps are no different from third-part apps and activities ● Native apps do not have higher-priorities and can be completely replaced with third-party apps/activities ● Android OS does not give anyone a priori advantageswww.youtube.com/vkedco www.vkedco.blogspot.com
  • 48. Intent Resolution ● Action – Name of the action being serviced – Name should be a unique string – Best practice is to use the Java package naming convention ● Category – When the action should be serviced ● Data – Data types that the action can work withwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 49. ContactPicker Project Demonstrates how to use intent on a native action and a URIwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 50. ContactPicker Project: Highlights // This is an example of how an intent is used // to launch a native Android action button.setOnClickListener(new OnClickListener() { public void onClick(View _view) { Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/")); startActivityForResult(intent, PICK_CONTACT); } });www.youtube.com/vkedco
  • 51. Feedback Bugs, errors to vladimir dot kulyukin at gmail dot comwww.youtube.com/vkedco www.vkedco.blogspot.com
  • 52. References and Reading Suggestions ● R. Meier. 2010. “Professional Android 2 Application Development,” Ch. 5, pp. 137-157, Wiley ● E. Gamma, R. Helm, R. Johnson, and J. Vlissides. 1995. “Design Patterns: Elements of Reusable Object-Oriented Software,” pp. 139-150, Addison-Wesley ● D. Musser, G. J. Derge, A. Saini. 2001. “STL Tutorial and Reference Guide”, 2nd Ed., pp. 40 – 43, Addison- Wesleywww.youtube.com/vkedco www.vkedco.blogspot.com

×