MobAppDev: ArrayAdapters, Activities & Activity Lifetimes

404 views
334 views

Published on

ArrayAdapters, Activities & Activity Lifetimes

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
404
On SlideShare
0
From Embeds
0
Number of Embeds
159
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MobAppDev: ArrayAdapters, Activities & Activity Lifetimes

  1. 1. MobAppDev Array Adapters, Activities & Activity Lifetimes Vladimir Kulyukin www.vkedco.blogspot.com
  2. 2. Outline ● ArrayAdapters ● Activities & Activity Lifetimes
  3. 3. Android Adapters
  4. 4. 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
  5. 5. 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
  6. 6. 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
  7. 7. 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
  8. 8. Sample Applications
  9. 9. 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
  10. 10. Pascal's Works: Screenshots Blaise Pascal
  11. 11. Pascal's Works 01 source code is here
  12. 12. 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
  13. 13. Code Highlights
  14. 14. 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);
  15. 15. 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);
  16. 16. 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(); }
  17. 17. Pascal's Works 02 source code is here
  18. 18. 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
  19. 19. Code Highlights
  20. 20. 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>
  21. 21. 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) );
  22. 22. Pascal's Works 03 source code is here
  23. 23. 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
  24. 24. Code Highlights
  25. 25. 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>
  26. 26. 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>
  27. 27. 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
  28. 28. 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
  29. 29. Pascal's Works 04 source code is here
  30. 30. 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
  31. 31. Adaptor Construction ArrayAdapter<CharSequence> adptr = ArrayAdapter.createFromResource(this, R.array.pascals_titles, R.layout.list_item_layout);
  32. 32. Activities & Their Lifetimes
  33. 33. 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
  34. 34. 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() { } }
  35. 35. Image Source: http://developer.android.com/guide/components/activities.html#Lifecycle

×