0% found this document useful (0 votes)
11 views

Adapters android

The document provides an overview of adapters and adapter views in Android, explaining their roles in connecting data sources to user interfaces. It details how to implement ListView and Spinner components using ArrayAdapter, including attributes and event handling for user interactions. Key classes and methods are highlighted for creating efficient and responsive UI elements that manage large datasets.

Uploaded by

sinubaby044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Adapters android

The document provides an overview of adapters and adapter views in Android, explaining their roles in connecting data sources to user interfaces. It details how to implement ListView and Spinner components using ArrayAdapter, including attributes and event handling for user interactions. Key classes and methods are highlighted for creating efficient and responsive UI elements that manage large datasets.

Uploaded by

sinubaby044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Adapters

• An adapter acts like a bridge between a data source and the user
interface.
• It reads data from various data sources, coverts it into View objects
and provide it to the linked Adapter view to create UI components.
• The data source or dataset can be an Array object, a List object etc.
• You can create your own Adapter class by extending the BaseAdapter
class, which is the parent class for all other adapter class.
• Android SDK also provides some ready-to-use adapter classes, such
as ArrayAdapter , CursorAdapter,SimpleAdapter ,
SimpleCursorAdapter etc.
Adapter View
• An Adapter View can be used to display large sets of data efficiently in
form of List or Grid etc. , provided to it by an Adapter.
• Adapter view is capable of displaying millions of items on the UI while
keeping the memory and CPU usage very low and without any
noticeable lag
• It only renders those view objects which are currently on screen,hence
saving memory
• Reuses already created layout to populate data items, as the user scrolls,
hence saving CPU usage
• Adapter takes the data from a data source and converting it into view
and then passing it to the AdapterView .
• The AdapterView is responsible for displaying the data
• List controls are classes that extend android.widget.AdapterView
and include ListView, GridView, Spinner, and Gallery

• The AdapterView class is inherited form ViewGroup class and


ListView, GridView and Spinner classes are derived from
AdapterView class.
ListView
• Android ListView is a view which groups several
items and display them in vertical scrollable list.
• The list items are automatically inserted to the list
using an Adapter that pulls content from a source
such as an array or database.
• ListView is implemented by importing
android.widget.ListView class
Attributes of ListView
• android:id
• android:divider →drawable or color to draw between list items
• android:dividerHeight→ specifies height of the divider
• android:entries →Specifies reference to an array resource that
will populate the ListView
• android:footerDividersEnabled →when set to false the ListView
won’t draw the divider before each footer view
• headerDividersEnabled → ListView won’t draw the divider
after each header view
• To display a list, include a list view in layout XML file:
<ListView
android:id="@+id/lsv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
• Place List of data in strings.xml file by creating string-array
in strings.xml
<string-array name="array_countries">
<item>Australia</item>
<item>Canada</item>
<item>Cambodia</item>
</string-array>
• Use ArrayAdapter to bind string values and listview
ArrayAdapter<CharSequence> adapter = ArrayAdapter .createFromResource(this,
R.array.array_countries, android.R.layout.simple_list_item_1);
• Arguments for this constructor −
• First argument this is the application context. Most of the case, keep
it this.
• Second argument is an array of strings which will be populated in
the text view.
• Final argument will be layout defined in XML file and
having TextView for each string in the array
• Once the array adapter created, then simply
call setAdapter() on ListView object as follows
ListView list=(ListView)findViewById(R.id.lsv);
list.setAdapter(adapter);
Spinner
• Spinners provide a quick way to select one value from a given set
of values and in the default state, a spinner only shows the
currently selected value
• When you touch(tap on) the spinner, it displays a dropdown menu
with all other available values(options), from which the user can
select a new one.
• ListView, on the other hand, is a view group that displays a list of
scrollable items.
• Add a spinner to the XML layout with the
• Spinner object
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
• Use ArrayAdapter to bind string values and listview
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter . createFromResource(this,
R.array.array-countries, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
• The createFromResource() method allows to create an ArrayAdapter from the string
array.
• The third argument for this method is a layout resource that defines how the selected
choice appears in the spinner control.
• The simple_spinner_item layout is provided by the platform and is the default layout
to use unless you'd like to define your own layout for the spinner's appearance.
• Then call setDropDownViewResource(int) to specify the layout the adapter should
use to display the list of spinner choices
• To define the selection event handler for a spinner, implement the
AdapterView.OnItemSelectedListener interface and the
corresponding onItemSelected() callback method
• This method has 4 parameters:
• AdapterView av: It's the Spinner view that you have used.
• View v: It defines the TextView inside the spinner that was clicked.
• int position: It tells the position of the item that was clicked in the
Spinner. The index or the position starts from 0.
• long id: It gives the row id of the item clicked in the Spinner. This
parameter is mainly used when dealing with databases in Android.
• onNothingSelected()
• This method is called whenever the currently selected item is removed
from the list of available items in the Spinner.
• spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long
id)
{
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// If an option is removed then what to do // or anything else
} });

You might also like