• Save
MobAppDev (Fall 2013): ArrayAdapters, Activities & Activity Lifetimes
Upcoming SlideShare
Loading in...5
×
 

MobAppDev (Fall 2013): ArrayAdapters, Activities & Activity Lifetimes

on

  • 540 views

 

Statistics

Views

Total Views
540
Views on SlideShare
392
Embed Views
148

Actions

Likes
0
Downloads
0
Comments
0

15 Embeds 148

http://vkedco.blogspot.com 101
http://www.vkedco.blogspot.com 16
http://vkedco.blogspot.in 8
http://reader.aol.com 7
http://vkedco.blogspot.de 3
http://vkedco.blogspot.mx 2
http://www.vkedco.blogspot.no 2
http://www.vkedco.blogspot.in 2
http://www.vkedco.blogspot.jp 1
http://www.blogger.com 1
http://www.vkedco.blogspot.co.uk 1
http://vkedco.blogspot.co.uk 1
http://vkedco.blogspot.it 1
http://vkedco.blogspot.com.es 1
http://vkedco.blogspot.com.au 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

MobAppDev (Fall 2013): ArrayAdapters, Activities & Activity Lifetimes MobAppDev (Fall 2013): ArrayAdapters, Activities & Activity Lifetimes Presentation Transcript

  • MobAppDev Array Adapters, Activities & Activity Lifetimes Vladimir Kulyukin www.vkedco.blogspot.com
  • Outline ● ArrayAdapters ● Activities & Activity Lifetimes
  • Android Adapters
  • Review: 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  Gallery
  • Review: 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
  • Review: Creating & Binding Adapters ● Define a data source (e.g., a array of Strings) ● Create an adapter and bind it to the data source ● Bind the adapter to a view that the adapter will populate
  • Blueprint for 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 objects
  • Sample Applications
  • Pascal's Works ● We will develop several applications that demonstrate various uses of ArrayAdapters ● All applications use ListViews and ArrayAdapters to display fives works by Blaise Pascal ● The titles of Pascal's works are placed into a String array ● Selection of an item in the ListView toasts the details of the item selected
  • Pascal's Works: Screenshots Blaise Pascal
  • Pascal's Works 01 source code is here
  • Pascal's Works 01 ● Develop an application that uses a ListActivity to display the titles of five books by Pascal 1) Discourses on the Condition of the Great; 2) The Art of Persuasion; 3) Of the Geometrical Spirit; 4) Preface to the Treatise on Vacuum; 5) New Fragment of the Treatise on Vacuum ● 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 selected
  • Code Highlights
  • Defining & Populating Data Source // 1. Create a data source this.mPascalsWorks = new String[NUM_WORKS]; this.mRes = this.getResources(); // 2. Populate the data source this.mPascalsWorks[0] = this.mRes.getString(R.string.discourses); this.mPascalsWorks[1] = this.mRes.getString(R.string.geometrical_spirit); this.mPascalsWorks[2] = this.mRes.getString(R.string.persuasion); this.mPascalsWorks[3] = this.mRes.getString(R.string.vacuum_01); this.mPascalsWorks[4] = this.mRes.getString(R.string.vacuum_02);
  • 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, mPascalsWorks); // 3. Bind the adapter to the list view lv.setAdapter(adptr);
  • 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 += "VIEW'S 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(); }
  • Pascal's Works 02 source code is here
  • Pascal's Works 02 ● Develop an application that uses a ListActivity to display the titles of five works by Blaise Pascal ● The titles are placed into a String array ● An ArrayAdapter is bound to the string array and attached to the ListActivity directly bypassing the creation of a ListView reference ● The ArraAdapter uses a custom view layout defined in list_item_layout.xml
  • Code Highlights
  • Custom TextView Item Layout <!-- This is saved in /res/layout/list_item_layoutxml --> <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>
  • Creating & Binding ArrayAdapter // Attach an anonymous ArrayAdapter // to the ListView of the ListActivity this.setListAdapter( new ArrayAdapter<String>(this, R.layout.list_item_layout, mPascalsWorks) );
  • Pascal's Works 03 source code is here
  • Pascal's Works 03 ● Develop an application that uses a ListActivity to display the titles of five works by Blaise Pascal and their fictional years ● The titles and years defined as XML arrays (pascals_works.xml) and are placed into String arrays at run time ● An ArrayAdapter is bound to the to the title string array and attached to the ListActivity directly bypassing the creation of a ListView reference ● At run time, the OnItemClickListener attached to ListView uses the TextView's position to retrieve the appropriate year from the array of fictional years
  • Code Highlights
  • Defining XML Arrays <!-- This is saved in /res/layout/pascals_works.xml --> <resources> <array name="pascals_titles"> <item>Discourses on the Condition of the Great</item> <item>The Art of Persuasion</item> <item>Of the Geometrical Spirit</item> <item>Preface to the Treatise on Vacuum</item> <item>New Fragment of the Treatise on Vacuum</item> </array> </resources>
  • Defining XML Arrays <!-- This is saved in /res/layout/pascals_works.xml --> <resources> <array name="pascals_titles_years"> <item>1655</item> <item>1656</item> <item>1657</item> <item>1658</item> <item>1659</item> </array> </array> </resources>
  • Inflating XML Arrays at Run Time static String[] mPascalsWorks = null; static int[] mPascalsYears = null; Resources res = this.getResources(); mPascalsWorks = res.getStringArray(R.array.pascals_titles); mPascalsYears = res.getIntArray(R.array.pascals_titles_years); source code is here
  • Consuming Item Clicks in ListView this.getListView().setOnItemClickListener( new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View child, int position, long id) { Toast t = Toast.makeText(getApplicationContext(), Integer.toString(mPascalsYears[position]), Toast.LENGTH_LONG); t.show(); }}); source code is here
  • Pascal's Works 04 source code is here
  • Pascal's Works 04 ● This application is the same as Pascal's Works 03 ● At run time, the OnItemClickListener attached to ListView uses the TextView's position to retrieve the appropriate year from the array of fictional years ● The only difference is in the construction of ArrayAdapter through a static method that takes a reference to the XML array of Pascal's titles and binds the constructed adapter to it
  • Adaptor Construction ArrayAdapter<CharSequence> adptr = ArrayAdapter.createFromResource(this, R.array.pascals_titles, R.layout.list_item_layout);
  • Activities & Their Lifetimes
  • Activity's Lifecycle ● An actvity's lifecycle is a set of states ● When the current state of an activity changes, the Android OS notifies the activity of that change ● The Android developer manages the activity's lifecycle by implementing the standard callback methods that are called on the activity object when its state changes (e.g., activity is created, stopped, resumed, and destroyed) ● Callback implementation is the only leverage the developer has over activities
  • Activity's Lifecycle: Callback Methods public class MyActivity { // The activity is created public void onCreate(Bundle savedInstanceState) { } // The activity is about to become visible protected void onStart() { } //The activity is visible (it is resumed) protected void onResume() { } // Another activity is taking focus (it is paused) protected void onPause() { } // The activity is no longer visible (it is stopped) protected void onStop() { } // The activity is about to be destroyed protected void onDestroy() { } }
  • Image Source: http://developer.android.com/guide/components/activities.html#Lifecycle