Adapters are the link between a set of data and the AdapterView that displays the data.
AdapterView
AdapterViews are ViewGroups that display child views given to it by an adapter. An example of an AdapterView is a ListView.
Adapters also provide the child views that display the data in the
AdapterView. Adapters are responsible for supplying the data and
creating the views representing each item Adapters get the data and pass it, alongwith a child view, to the parent AdapterView which displays the child view and the data
The Android Framework has a set of native adapters that are easy to
use. You can also create your own custom adapters if you wish.
Android Adapter:
Adapters call the getView()
method which returns a view for each item within the adapter view. The
layout format and the corresponding data for an item within the adapter
view is set in the getView() method. Now, it will be a performance nightmare if getView()
returns a new View every time it is called. Creating a new view is very
expensive in Android as you will need to loop through the view
hierarchy (using the find ViewbyID () method) and then
inflate the view to finally display it on the screen.It also puts a lot
of pressure on the garbage collector. That is because when the user is
scrolling through the list, if a new view is created; the old view
(since it is not recycled) is not referenced and becomes a candidate to
be picked up by the garbage collector. So what Android does is that it
recycles the views and reuses the view that goes out of focus.
Creating a custom Adapter View:
@Override publicView getView(intitempos, View convertView, ViewGroup parent) { //Check if the convertview is null,
//if it is null it probably means that this is the first time the view has been displayed if (convertView == null) { convertView = View.inflate (context,R.layout.list_content_layout, null); } //If it is not null, you can just reuse it from the recycler TextView txtcontent = (TextView) convertView.findViewById(R.id.textView1); <code>ImageView imgcontent = (ImageView) </code>convertView<code>.findViewById(R.id.imageView1); </code> <code> </code> <code>Paintings paintingcontent = content [itempos]; </code> txtcontent.setText (paintingcontent.imagetitle); imgcontent.setImageResource(paintingcontent.drawableresid); // return the view for a single item in the listview returnconvertView;
Two of Android’s common adapters are:
ArrayAdapter
An ArrayAdapter is an adapter backed by an array of objects. It links the array to the Adapter View.The default ArrayAdapter converts an array item into a String object putting it into a TextView. The text view is then displayed in the AdapterView (a ListView for example).
When you create the adapter, you need to supply the layout for displaying each array string. You can define your own or use one of Android’s, such as:
android.R.layout.simple_list_item_1
There are alternative constructors that you can use for more complex layouts. You can also display images instead of strings.SimpleCursorAdapter
The SimpleCursorAdapter links the data contained in a Cursor to an Adapter View.A cursor is a set of data. You usually get a cursor when you do a database query. The result of your query is contained in the cursor. The SimpleCursorAdapter binds the Cursor data to an Adapter View. You define a layout that controls how each row of data is displayed.
Each row’s view is populated using the column values of the corresponding row in the cursor.
Other useful Adapters
CursorAdapter
A CursorAdapter links a Cursor’s data to a List View. You must include the database’s _id column as it’s used in processing the list item’s selection.The SimpleCursorAdapter is a subclass of CursorAdapter.
The SimpleCursorAdapter is easier to use while the CursorAdapter requires more work but allows more customization.
SimpleAdapter
The SimpleAdapter links static data to views defined in a layout file. You specify the data as an ArrayList of Maps. Each entry in the ArrayList will display as a row in a list.BaseAdapter
The BaseAdapter is a common base class for an Adapter that can be used in a ListView and a Spinner.ListAdapter
The ListAdapter links the data and a ListView displaying the data. The List View can display any data type provided it’s wrapped in a ListAdapter.ListView
A ListView is a ViewGroup that displays a list of scrollable items. Items are inserted in the list using an Adapter.The Adapter gets the data from a source such as an array, converts each item into a view and places the view in the list.
GridView
A GridView is a ViewGroup that display items in a 2 dimensional scrollable grid.An Adapter gets the items from a data source, creates a view to enclose the item and then inserts the view in the parent grid.
No comments:
Post a Comment