Arrays and Adapter Prepared by Ms Ayesha Khaliq

Arrays and Adapter Prepared by Ms Ayesha Khaliq

Arrays • Set of collection of similar data • We can declare list of items in values folder which contains strings. xml file • Declaration of array in java For Example String[] string. Array; string. Array = my. Resources. get. String. Array(R. array. string_array); int[] int. Array = my. Resources. get. Int. Array(R. array. integer_array);

Arrays <string-array name=“string_array“> <item>Item 1</item> <item>Item 2</item> <item>Item 3</item> </string-array> <array name=“integer_array“> <item>3</item> <item>2</item> <item>1</item> </array>

Adapters • Adapters provide a common interface to the data model behind a selectionstyle widget, such as a listbox. • In most cases you won’t have to create your own Adapters from scratch. Android supplies a set of Adapters that can pump data from common data sources (including arrays and Cursors) into the native controls that extend Adapter View • Two of the most useful and versatile native Adapters: 1) Array. Adapter 2) Simple. Cursor. Adapter

Array. Adapter • The Array Adapter uses generics to bind an Adapter View to an array of objects of the specifi ed class. By default, the Array Adapter uses the to. String value of each object in the array to create and populate Text Views. • The Array. Adapter constructor takes three parameters: 1) The Context to use (typically this will be your activity instance) 2) The resource ID of a view to use (such as a built-in system resource ID, as previously shown) 3) The actual array or list of items to show
![Example String[] items={“red", “blue", “green", “yellow"}; new Array. Adapter<String>(this, android. R. layout. simple_list_item_1, items); Example String[] items={“red", “blue", “green", “yellow"}; new Array. Adapter<String>(this, android. R. layout. simple_list_item_1, items);](http://slidetodoc.com/presentation_image_h2/a32c0da15e846cdd532ce3f6f2865fc3/image-6.jpg)
Example String[] items={“red", “blue", “green", “yellow"}; new Array. Adapter<String>(this, android. R. layout. simple_list_item_1, items); • We can use subclass Array. Adapter and override get. View() to “roll your own” views:

Example public View get. View(int position, View convert. View, View. Group parent) { if (convert. View==null) { convert. View=new Text. View(this); } convert. View. set. Text(build. String. For(position)); return(convert. View); }

Another Example • <? xml version="1. 0" encoding="utf- android: layout_height="wrap_conten 8"? > t" /> <Linear. Layout <Spinner android: id="@+id/spinner" xmlns: android="http: //schemas. android: layout_width="fill_parent" id. com/apk/res/android" android: layout_height="wrap_conten android: orientation="vertical" t" android: layout_width="fill_parent" android: draw. Selector. On. Top="true" android: layout_height="fill_parent" > /> <Text. View </Linear. Layout> android: id="@+id/selection" android: layout_width="fill_parent"

Another Example public class Spinner. Demo extends Activity implements Adapter. View. On. Item. Selected. Listener { Text. View selection; String[] items={"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "morbi", "vel", "ligula", "vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat", "placerat", "ante", "porttitor", "sodales", "pellentesque“};

Another Example @Override public void on. Create(Bundle icicle) { super. on. Create(icicle); set. Content. View(R. layout. main); selection=(Text. View)find. View. By. Id(R. id. selection); Spinner spin=(Spinner)find. View. By. Id(R. id. spinner); spin. set. On. Item. Selected. Listener(this); Array. Adapter<String> aa=new Array. Adapter<String>(this, android. R. layout. simple_spinner_item, items); aa. set. Drop. Down. View. Resource( android. R. layout. simple_spinner_dropdown_item); spin. set. Adapter(aa); }

Another Example public void on. Item. Selected(Adapter. View<? > parent, View v, int position, long id) { selection. set. Text(items[position]); } public void on. Nothing. Selected(Adapter. View<? > parent) { selection. set. Text(""); } }
- Slides: 11