First, let's take a look at the architecture of the adapter:
The object of an adapter plays the role of a bridge. This bridge connects an adapterview and the data it contains. The adapter provides
Channels. The adapter also generates a view for each data item in the dataset. It has an important method: Public
Abstract view getview (INT position, view convertview, viewgroup
Parent ). This method is indirectly called by setlistadapter. Getview
The function of the method is to get a view, which displays the data at the specified position in the data item. You can manually create a view or
Inflate in layout. When this view is inflated, its parent view (such as gridview and listview) will use the default
Layout parameter, unless you use the inflate (INT, Android. View. viewgroup, Boolean) method to specify a Root View and prevent attachment
On the root.
The following describes several common subclasses:
Listadapter interface: inherited from adapter. Listadapter is a bridge between listview and list data. Data often comes from a cursor, but this is not necessary. Listview can display any data, as long as it is packaged by a listadapter.
Baseadapter abstract class: it is a general base class that implements the adapter class that can be used in listview (listadapter interface implemented) and spinner (interface implemented by spinner.
Arrayadapter class: A listadapter that manages such a listview: This listview is supported by an array. This array can be any
Object. By default, this class is expected to be like this: the provided resource ID is associated with a separate textview. If you want to use a more complex layout, you need to use the structure containing the domain ID
Create a function. This field ID can be associated with a textview in a larger layout resource. It will be filled by the tostring () method of each object in the array. You can
Add the lists or arrays of a common object. Override the tostring () method of your object to determine which text with data written in the list will be displayed. If you want to use some other
Different from textview views to display arrays (such as imageviews), or want to have data other than the values of tostring () returned values in views,
You need to override the getview (INT, view, viewgroup) method to return the desired view type.
Simpleadapter class: A simple adapter that maps static data with views defined in XML. You can specify the data on the list as
The arraylist of the map model. Each entry in the arraylist corresponds to a row in the list. Maps contains the data of each row. You must specify an XML file.
XML defines the view used to display a row. You need to specify a ing from the map keys to the specified views. Binding data to views occurs in two scenarios:
Stages:
If a simpleadapter. viewbinder is available
Setviewvalue (Android. View. View, object, string) is called. If true is returned, the binding occurs. If
False, the following views will be tried in order:
~ Implements the checkable view (such as checkbox). The expected binding value is boolen.
~ Textview. The expected binding value is string and the setviewtext method is called.
~ Imageview. The expected binding value is the ID or string of a resource. The setviewimage method is called.
If no suitable binding is found, an illegalstateexception is thrown.
The following is an example of simpleadapter:
/* -------------------- Mail. xml ---------------------------*/
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Id = "@ + ID/mylistview">
/* -------------------- Simple_list_item_2.xml ---------------------------*/
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ + ID/widget0"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
Android: Id = "@ + ID/itemimage"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">
Android: Id = "@ + ID/itemtitle"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "textview"
Android: layout_x = "50px"
Android: textsize = "15pt">
Android: Id = "@ + ID/itemtext"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "textview"
Android: layout_x = "50px"
Android: layout_y = "40px">
Android: Id = "@ + ID/itemcheck"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_x = "270px">
/* ------------------ Testuilist. Java ---------------------------*/
Public class testuilist extends activity {
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Listview list = (listview) findviewbyid (R. Id. mylistview );
Arraylist> lstimageitem = new arraylist> ();
String [] str1 = {"row one", "row two", "row three", "row four "};
String [] str2 = {"first line", "second line", "third line", "fourth line "};
For (INT I = 0; I <str1.length; I ++ ){
Hashmap map = new hashmap ();
Map. Put ("itemimage", R. drawable. Icon );
Map. Put ("itemtitle", str1 [I]);
Map. Put ("itemtext", str2 [I]);
Lstimageitem. Add (MAP );
}
Simpleadapter saimageitems = new simpleadapter (this,
Lstimageitem,
R. layout. simple_list_item_2,
New String [] {"itemimage", "itemtitle", "itemtext "},
New int [] {R. Id. itemimage, R. Id. itemtitle, R. Id. itemtext });
List. setadapter (saimageitems );
}
}
If each row of your listview is to be clicked, the event will be returned. The most effective method is to inherit a listactivity. Listactivity is
Such activity: this activity can be bound to a data source such as array or cursor to display the list of some items, and when the user selects
An item can expose the event handle.
Listactivity has a listview object. This listview object can be bound to different data sources, especially an array or a cursor with query results. Listactivity has three usage methods: binding and screen.
Layout and row layout. The following will only discuss screen layout:
Listactivity has a default layout. This
Layout is a separate, full-screen list in the center of the screen. However, if you want to, you can set your own view by calling the setcontentview () method in oncreate ().
Layout. To do this, your view must contain a "@ Android: ID/Android: List" (or a list object is included in the Code ).
Randomly, When you specify that the view is empty, you can include any type of view object for display. The "empty list" notification must have an id "Android: empty ".
Note: The setlistadapter (adapter) method must be called to display the list bound to data through the adapter.
The setlistadapter method indirectly calls the getview method of the adapter to return the desired view type. And when you click
Item, the listview is repainted Based on getview. For examples, see android
The example of "Resource Manager" In SDK development.
To implement event listening, rewrite protected void onlistitemclick (listview L, view V, int
Position, long ID) method.
To display a custom View row in XML in the listactivity custom listview row by row, and dynamically bind data to each row
I have written a myadapter class. This adapter inherits baseadapter and its constructor has at least one list parameter to realize dynamic data binding. There are two important
Steps:
1) rewrite the getview method. An important step is to use the items. Get (position) method to obtain the passed data. Among them, items is
List, which is assigned the value of the input list parameter. Position is the number of rows in the listview. Get returns the E type, that is, the template type in the list.
2) write an internal class private class
Viewholder. This internal class only contains member variables, which are the small view components you want to display in a row of listview.
To dynamically input data of other classes in the adapter, you must add another (or more) List parameter to the constructor.
Finally, we provide some examples of listening events using myadapter and listactivity:
/* -------------------- Myhome. Java -----------------------*/
Package com. Li. Android. myhome;
Import java. util. arraylist;
Import java. util. List;
Import Android. App. alertdialog;
Import Android. App. listactivity;
Import Android. content. dialoginterface;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. listview;
Public class myhome extends listactivity
{
Private list items = NULL;
Protected void oncreate (bundle icicle)
{
Super. oncreate (icicle );
Setcontentview (R. layout. Main );
Items = new arraylist ();
String [] titles =
{"Default theme", "theme-a", "theme-B", "Mid-Autumn Festival", "Pink girl", "Beautiful year "};
For (INT I = 0; I <titles. length; I ++)
{
String title = titles [I];
Items. Add (title );
}
Setlistadapter (New myadapter (this, items ));
}
@ Override
Protected void onlistitemclick (listview L, view V, int position,
Long ID)
{
New alertdialog. Builder (myhome. This). setitems (
R. array. items_my_dialog, new dialoginterface. onclicklistener ()
{
Public void onclick (dialoginterface dialog, int whichcountry)
{
}
})
. Show ();
}
}
/* -------------------- Myadapter. Java -----------------------*/
Package com. Li. Android. myhome;
Import java. util. List;
Import Android. content. context;
Import Android. Graphics. Bitmap;
Import Android. Graphics. bitmapfactory;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. widget. baseadapter;
Import Android. widget. imageview;
Import Android. widget. textview;
Public class myadapter extends baseadapter
{
Private layoutinflater minflater;
Private bitmap icom_theme;
Private bitmap icon_selected32;
Private list items;
Public myadapter (context, list it)
{
Minflater = layoutinflater. From (context );
Items = it;
Icom_theme = bitmapfactory. decoderesource (context. getresources (),
R. drawable. theme );
Icon_selected32 = bitmapfactory. decoderesource (Context
. Getresources (), R. drawable. selected32 );
}
Public int getcount ()
{
Return items. Size ();
}
Public object getitem (INT position)
{
Return items. Get (position );
}
Public long getitemid (INT position)
{
Return position;
}
Public View getview (INT position, view convertview, viewgroup parent)
{
Viewholder holder;
Convertview = minflater. Inflate (R. layout. file_row, null );
Holder = new viewholder ();
Holder. Text = (textview) convertview. findviewbyid (R. Id. Text );
Holder. mtheme = (imageview) convertview. findviewbyid (R. Id. theme );
Holder. mselected32 = (imageview) convertview
. Findviewbyid (R. Id. selected32 );
Holder. mtheme. setimagebitmap (icom_theme );
String title = items. Get (position );
Holder. Text. settext (title );
Holder. mselected32.setimagebitmap (icon_selected32 );
Return convertview;
}
Private class viewholder
{
Textview text;
Imageview mtheme;
Imageview mselected32;
}
}
/* -------------------- Main. xml -----------------------*/
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
Android: Id = "@ Android: ID/List"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
>
/* -------------------- File_row.xml -----------------------*/
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ + ID/widget0"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
>
Android: Id = "@ + ID/text"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_x = "50px"
Android: textsize = "15pt">
Android: Id = "@ + ID/selected32"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_x = "270px">
/* -------------------- String. xml -----------------------*/