Android Ui Adapter
Android Ui Adapter
Disclaimer
Portions of this presentation are modifications based on work created and shared by the Android Open Source Project
> http://code.google.com/policies.html
They are used according to terms described in the Creative Commons 2.5 Attribution License
> http://creativecommons.org/licenses/by/2.5/
Topics
AdapterView & Adapter AdapterView responsibilities ListActivity, ListView, and ListAdapter Spinner Gallery
Typically you are going to use subsclasses of AdapterView class instead of using it directly Example subclasses of AdapterView class
> ListView > Spinner > Gallery
What is an Adapter?
An Adapter object acts as a bridge between an AdapterView object and the underlying data for that view.
> The Adapter provides access to the data items.
The Adapter is also responsible for making a View for each item in the data set. Types of Adatpers - they implements ListAdatper interface
> ArrayAdatper > CursorAdatper > There are a few more
6
AdapterView Responsibilities
AdapterView Responsibilities
Two main responsibilities of AdapterView
> Filling the layout with data (it received through the
help of an Adapter) > Handling user selections - when a user selects an item, perform some action
10
ListView
12
ListView Class
A child class of AdapterView class Shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view
13
14
16
17
through an adatper to different data sources, typically either an array or a Cursor holding query results. > setListAdapter(ListAdatper adapter) method automatically creates ListView object from the ListAdapter object
Has a default layout that consists of a single, full-screen list in the center of the screen
18
19
Spinner
20
Spinner Class
A child class of AdapterView class Displays one child at a time and lets the user pick among them. The items in the Spinner come from the Adapter associated with this view There is NO special SpinnerActivity class, so you have to create Spinner object yourself
21
Example of Spinner
public class HelloSpinner extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.planets_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
22
Gallery
24
Gallery Class
A child class of AdapterView class A view that shows items in a centerlocked, horizontally scrolling list.
25
Example of Gallery
public class HelloGallery extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Gallery g = (Gallery) findViewById(R.id.gallery); g.setAdapter(new ImageAdapter(this)); g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show(); } });
26
27
Thank you!