• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Mobile Application Development: Lecture 10
 

Mobile Application Development: Lecture 10

on

  • 995 views

 

Statistics

Views

Total Views
995
Views on SlideShare
504
Embed Views
491

Actions

Likes
1
Downloads
0
Comments
0

40 Embeds 491

http://vkedco.blogspot.com 224
http://www.vkedco.blogspot.com 119
http://vkedco.blogspot.in 37
http://vkedco.blogspot.co.uk 15
http://vkedco.blogspot.com.es 13
http://vkedco.blogspot.kr 11
http://www.vkedco.blogspot.in 6
http://vkedco.blogspot.hu 5
http://vkedco.blogspot.ae 4
http://vkedco.blogspot.fr 4
http://vkedco.blogspot.co.il 4
http://vkedco.blogspot.com.br 3
http://vkedco.blogspot.ru 3
http://vkedco.blogspot.ca 3
http://vkedco.blogspot.se 3
http://vkedco.blogspot.it 3
http://vkedco.blogspot.pt 3
http://vkedco.blogspot.de 2
http://vkedco.blogspot.ie 2
http://vkedco.blogspot.jp 2
http://vkedco.blogspot.co.nz 2
http://vkedco.blogspot.be 2
http://vkedco.blogspot.com.au 2
http://www.blogger.com 2
http://vkedco.blogspot.com.ar 2
http://vkedco.blogspot.nl 1
http://translate.googleusercontent.com 1
http://vkedco.blogspot.gr 1
http://vkedco.blogspot.com.tr 1
http://vkedco.blogspot.mx 1
http://vkedco.blogspot.no 1
http://vkedco.blogspot.tw 1
http://vkedco.blogspot.dk 1
http://www.vkedco.blogspot.co.il 1
http://vkedco.blogspot.ch 1
http://www.vkedco.blogspot.co.uk 1
http://www.vkedco.blogspot.ch 1
http://www.vkedco.blogspot.fr 1
http://www.vkedco.blogspot.gr 1
http://www.vkedco.blogspot.ru 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Mobile Application Development: Lecture 10 Mobile Application Development: Lecture 10 Presentation Transcript

    • Mobile Application Development Lecture 10 Vladimir Kulyukin Department of Computer Science Utah State Universityhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • Outline ● Adapter Design Pattern ● Android Adapters ● ListView ● ArrayAdapters – Binding ArrayAdapters to data sources – Attaching ArrayAdapters to Viewshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Adapter Design Pattern ● The intent of the Adapter Design Pattern is to convert the interface of one class into another interface clients expect ● 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 wrappershttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • 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 unpredictable 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 classhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • 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 Adapter: Adapts the interface of Adaptee to Targethttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • Adapter DP Implementation: Composition Client Target Adaptee Request() SpecificRequest() Adapter Request() SpecificRequest() Target: Defines Domain-Specific Interface Client: Uses Target Objects Adaptee: Defines Interface that must be adapted Adapter: Adapts the interface of Adaptee to Targethttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • Android Adapters ● Android adapters provide a common interfaces to the data model behind containers ● More specifically, Android adapters provide child views for specific containers ● Android adapters are used with container controls that extend android.widget.AdapterView: – ListView – GridView – Spinner – Galleryhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • 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 controlshttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • Creating & Binding Adapters ● Define a data source (e.g., a array of Strings) ● Create an adapter and binding it to the data source ● Bind the adapter to a viewhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • Creating & Binding Adapters // 1. Create a data source String[] ary = { “string_0”, “string_1”, “string_2” }; // 2. Create & bind adapter ArrayAdapter<String> adptr = new ArrayAdapter<String>(this, android.R.layout_sipmle_list_item_1, ary); // 1st argument is Context (this) // 2nd argument is the view layout // 3rd argument is a sequence of objectshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Example 01 ● Develop an application that uses a ListActivity to display the titles of five books by Jalaluddin Rumi (“Spiritual Couplets,” “The Works of Shams of Tabriz,” “In It Whats In It,” “Seven Sessions,” “The Letters”) ● The titles are placed into a String array ● An ArrayAdapter is bound to the string array and attached to the ListView of the ListActivity ● Selection of an item in the ListView toasts the details of the item selectedhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • ListViewArrayAdapter1 Code Snippetshttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • Defining & Populating Data Source // 1. Create a data source String[] mRumiWorks = null; Resources mRes = null; mRumiWorks = new String[5]; mRes = getResources(); // 2. Populate the data source mRumiWorks[0] = mRes.getString(R.string.spiritual_couplets); mRumiWorks[1] = mRes.getString(R.string.the_works_of_shams_of_tabriz); mRumiWorks[2] = mRes.getString(R.string.in_it_whats_in_it); mRumiWorks[3] = mRes.getString(R.string.seven_sessions); mRumiWorks[4] = mRes.getString(R.string.the_letters);http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Creating & Binding ArrayAdapter // 1. Get the ListView of the ListActivity ListView lv = this.getListView(); // 2. Create and bind an ArrayAdapter 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);http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Attach and Implement OnItemClickListener lv.setOnItemClickListener(this); public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String msg = "PARENT = " + parent.toString() + "n"; msg += "VIEW = " + view.toString() + "n"; msg += "VIEWS TEXT = " + ((TextView)view).getText().toString() + "n"; msg += "POSITION = " + Integer.toString(position) + "n"; msg += "ID = " + Long.toString(id); // Make a toast and display it on the screen Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Example 02 ● Develop an application that uses a ListActivity to display the titles of five books by Jalaluddin Rumi (“Spiritual Couplets,” “The Works of Shams of Tabriz,” “In It Whats In It,” “Seven Sessions,” “The Letters”) ● The titles are placed into a String array ● An ArrayAdapter is bound to the string array and attached to the ListActivity directly bypassing the ListView ● The ArraAdapter uses a custom view layouthttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • ListViewArrayAdapter2 Code Snippetshttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • Custom TextView Item Layout <!-- This is saved in /res/layout/list_item_specs.xml --> <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="10sp"> </TextView>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Creating & Binding ArrayAdapter // Attach an anonymous ArrayAdapter // to the ListView of the ListActivity this.setListAdapter( new ArrayAdapter<String>(this, R.layout.list_item_specs, mRumiWorks) );http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Example 03 ● Develop an application that uses a ListActivity to display the titles of five books by Jalaluddin Rumi (“Spiritual Couplets,” “The Works of Shams of Tabriz,” “In It Whats In It,” “Seven Sessions,” “The Letters”) both in English and Persian ● The English and Persian titles are placed into String arrays ● The string arrays are defined statically via XML and inflated in onCreate() ● An ArrayAdapter is bound to the English string array and is attached to a ListView ● A selection of an English title toasts its Persian equivalenthttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • ListViewArrayAdapter3 Code Snippetshttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • Defining XML Arrays <!-- This is saved in /res/layout/rumi_works.xml --> <resources> <array name="rumi_works_english"> <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>http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Creating & Binding ArrayAdapter static String[] mRumiWorksEnglish = null; static String[] mRumiWorksPersian = null; // 1. Get a Resources object Resources res = getResources(); // 2. Inflate two arrays from XML resources mRumiWorksEnglish=res.getStringArray(R.array.rumi_works_english); mRumiWorksPersian=res.getStringArray(R.array.rumi_works_persian); // 3. Create and bind an adapter setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item_specs, mRumiWorksEnglish));http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Example 04 ● Develop an application that uses a ListActivity to display the titles of five books by Jalaluddin Rumi (“Spiritual Couplets,” “The Works of Shams of Tabriz,” “In It Whats In It,” “Seven Sessions,” “The Letters”) both in English and Persian ● The English and Persian titles are placed into String arrays ● An ArrayAdapter is bound to the English string array defined in XML at creation and is attached to a ListView ● A selection of an English title toasts its Persian equivalenthttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • ListViewArrayAdapter4 Code Snippetshttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • Creating & Binding ArrayAdapter // Create an array adapter directly from an XML resource that // defines a string array (/res/values/rumi_works.xml). // The adapter also uses /res/layout/list_item_specs.xml to customize // the appearance of each child text view in the parent list view // container. ArrayAdapter<CharSequence> adptr = ArrayAdapter.createFromResource(this, R.array.rumi_works_english, R.layout.list_item_specs); this.setListAdapter(adptr);http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Example 05 ● Develop an application that uses a ListActivity to display the English translations of the following quatrains by Jalaluddin Rumi: 12, 77, 116, 494, and 549 ● The translations are taken from “Open Secret: Versions of Rumi” by John Moyne and Coleman Barks ● The app has two activities: QuatrainSelector and QuatrainDisplayer ● QuatrainSelector uses an ArrayAdapter to bind quatrain numbers to a ListView and starts QuatrainDisplayer via an explicit intenthttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • Example 05 ● QuatrainDisplayer does the following: – Extracts the quatrain number from the explicit intent – Loads the appropriate quatrain from an XML string array – Adds each line of the quatrain into its multi- line EditTexthttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • RumiQuatrainViewer1 Code Snippetshttp://www.youtube.com/vkedco www.vkedco.blogspot.com
    • Creating & Binding ArrayAdapter Resources mRes = getResources(); String[] mQuatrainNumbers = null; mQuatrainNumbers = mRes.getStringArray(R.array.quatrain_numbers); // 1. Get the list view of the current activity ListView lv = this.getListView(); lv.setBackgroundColor(Color.WHITE); // 2. Create an adapter ArrayAdapter<String> adptr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.mQuatrainNumbers); // 3. Bind the adapter to the list view lv.setAdapter(adptr);http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
    • Feedback Bugs to vladimir dot kulyukin at gmail dot comhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com