Android Development: Advanced Widgets using Adapters Richard S. Stansbury 2015
Adapters ListView Spinner GridView ExpandableListView Gallery These all extend AdapterView http://developer.android.com/reference/android/widget/AdapterView.html
Adapters Adapters store content that are then displayed by the AdapterView classes e.g. ArrayAdapter CursorAdapter ListAdapter SpinnerAdapter
ArrayAdapter ArrayAdapter (Context context, int resource, T [] objects) context – context from which you are building the ArrayAdapter resource – R.id.<identifier> specifies the view that is used to encapsulate the data objects – objects to be displayed toString() is performed on each item in the object array and the appropriate TextView derivative is generated Example: ArrayAdapter a = new ArrayAdapter(this, android.R.Layout.simple_list_item_1, items); http://developer.android.com/reference/android/widget/AdapterView.html
ListView Displays a list of strings List items can be selected or clicked extending ListActivity is an easy approach, but requires a different type of ID XML <ListView android:id = “android:id/list” android:layout_width = “fill_parent” android:layout_height = “wrap_content” >
ListView (continued) Java setAdapter(…) setOnItemSelectedListener(…) Sets the adapter to display setOnItemSelectedListener(…) Sets the listener for when an item is selected setOnItemClickListener(…) Sets the listener for an item being clicked setOnScrollListener(…) Records scroll event getSelectedItem() Returns the object for the currently selected item.
Example How do we specify a list? Ignore the multiple lines per item…this was a default of the layout viewer
ListView: Selection Modes As an alternative to tracking clicks Single selection or multiple selection Java: setChoiceMode(mode) mode = {CHOICE_MODE_SINGLE, CHOICE_MODE_MULTIPLE} XML: android:choiceMode = “multipleChoice” Resource ID: android.R.layout.simple_list_item_single_choice android.R.simple_list_item_multiple_chioce
Example
Spinner A fancy term for a drop down menu i.e. a List View that is initially collapsed and when clicked expands providing a scrollable view of the list <spinner android:id=“@id+/spinner” android:layout_width = “fill_parent” android:layout_height = “fill_parent” />
Spinner (continued) Java setAdapter setOnItemSelectedListener setDropDownViewResource - used to configure the adapter (not spinner)
Example
Example
Challenge Build / Demo Note: cropped from full portrait view to fit on slides.