Search This Blog

Friday, 16 March 2012

Chapter 15:Array. Adapters


Adapters

Adapters are basically used to deliver content. One adapter you probably have in every application is the CursorAdapter which enables you to deliver content given by a cursor from a database query. A ListView has nearly always some sort of Adapter.Well adapter in android are basically bridge between UI components and the datasource that fill data into UI Component.Like Lists (UI Component) gets populated by using list adapter , from a datasource array.
Adapters
A ListView gets its data assigned to via an an adapter. This adapter also defines which layout is used for the individual rows of the list and how the data is assigned to the Views in the row layout.
The adapter is assigned to the list via the setAdapter method on the ListView object.
An adapter which must extend BaseAdapter. Android provides several standard adapters; the most important are ArrayAdapter and CursorAdapter.
ArrayAdapter can handle data based on Arrays or Lists while SimpleCursorAdapter handle database related data.
Android provides already some default layouts for rows in ListViews which can be used in an adapter, e.g. android.R.layout.simple_list_item1.
The following shows an example of using ArrayAdapter in an Activity assuming that the layout
contains a ListView with the "mylist" ID.
                                
ListView listView = (ListView) findViewById(R.id.mylist);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        "Linux", "OS/2" };

// First paramenter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the View to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1, values);

// Assign adapter to ListView
listView.setAdapter(adapter);

No comments:

Post a Comment