In development, we need to bind some data to the desktop. This requires AdapterView. AdapterView is a subclass of ViewGroup. It determines how to bind a view to a special data type through the Adapter. AdapterView is very helpful when you display data in your layout. Gallery, ListView, and Spinner are subclasses of AdapterView.
The structure of AdapterView is as follows:
Then let's look at the structure of the Adapter:
The above section fully demonstrates the basic relationships between their child classes and their parent classes.
The following is an example of ListViewDemo:
Let's look at a simple adapter example:
Public class SimpleList extends ListActivity {
Private String [] mListString = {"Name: Wang kuifeng", "Gender: male", "Age: 23 ",
"Place of residence: Putuo District, Shanghai", "Email: wangkuifeng0118@126.com "};
Private ListView mListView = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
MListView = this. getListView ();
SetListAdapter (new ArrayAdapter <String> (this,
Android. R. layout. simple_list_item_1, mListString ));
MListView. setOnItemClickListener (new OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <?> Parent, View view,
Int position, long id ){
// TODO Auto-generated method stub
Toast. makeText (SimpleList. this, "you selected:" + mListString [position], 1). show ();
}
});
}
The System-defined adaptation mode is used here. Of course, this mode can only be used for simple data adaptation. The following describes the effect:
Next, let's look at how SimpleAdapter adapts to a slightly complex one:
Public class IconList extends ListActivity {
Private String [] mListTitle = {"name", "gender", "Age", "Place of Residence", "email "};
Private String [] mListStr = {"Wang Qifeng", "male", "23", "Putuo District, Shanghai ",
"Wangkuifeng0118@126.com "};
ListView mListView = null;
ArrayList <Map <String, Object> mData = new ArrayList <Map <String, Object> ();;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
MListView = getListView ();
Int lengh = mListTitle. length;
For (int I = 0; I <lengh; I ++ ){
Map <String, Object> item = new HashMap <String, Object> ();
Item. put ("image", R. drawable. portrait );
Item. put ("title", mListTitle [I]);
Item. put ("text", mListStr [I]);
MData. add (item );
}
SimpleAdapter adapter = new SimpleAdapter (this, mData, R. layout. iconlist,
New String [] {"image", "title", "text"}, new int [] {R. id. image, R. id. title, R. id. text });
SetListAdapter (adapter );
MListView. setOnItemClickListener (new OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <?> Parent, View view,
Int position, long id ){
// TODO Auto-generated method stub
Toast. makeText (IconList. this, "you selected the title:" + mListTitle [position] + "content:" + mListStr [position], Toast. LENGTH_LONG). show ();
}
});
Super. onCreate (savedInstanceState );
}
}
The above data can be read from the same database or from the network. I will not introduce it too much here to see the effect:
Haha looks more beautiful. If you want to make a more complex layout, you need to use BaseAdapter. Take a look at the layout file:
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" android: layout_height = "wrap_content">
<ImageView android: id = "@ + id/color_image"
Android: layout_width = "wrap_content" android: layout_height = "fill_parent"
Android: layout_alignParentTop = "true" android: layout_alignParentBottom = "true"
Android: adjustViewBounds = "true"
Android: padding = "2dip"/>
<TextView android: id = "@ + id/color_title"
Android: layout_width = "fill_parent" android: layout_height = "wrap_content"
Android: layout_toRightOf = "@ + id/color_image"
Android: layout_alignParentTop = "true"
Android: layout_alignParentRight = "true" android: singleLine = "true"
Android: ellipsize = "marquee"
Android: textSize = "15dip"/>
<TextView android: id = "@ + id/color_text"
Android: layout_width = "fill_parent" android: layout_height = "wrap_content"
Android: layout_toRightOf = "@ + id/color_image"
Android: layout_below = "@ + id/color_title"
Android: layout_alignParentBottom = "true"
Android: layout_alignParentRight = "true"
Android: singleLine = "true"
Android: ellipsize = "marquee"
Android: textSize = "20dip"/>
</RelativeLayout>
The following is the core code:
Public class ColorList extends ListActivity {
Private String [] mListTitle = {"name", "gender", "Age", "Place of Residence", "email "};
Private String [] mListText = {"Wang kuifeng", "male", "23", "Putuo District, Shanghai", "wangkuifeng0118@126.com "};
Private ListView mListView = null;
Private MyListAdapter myAdapter = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
MListView = this. getListView ();
MyAdapter = new MyListAdapter (this );
This. setListAdapter (myAdapter );
MListView. setOnItemClickListener (new OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <?> Parent, View view,
Int position, long id ){
// TODO Auto-generated method stub
View v = parent. getChildAt (position );
V. setBackgroundColor (Color. RED );
Toast. makeText (ColorList. this, "you selected" + mListText [position], 1). show ();
}
});
Super. onCreate (savedInstanceState );
}
Private class MyListAdapter extends BaseAdapter {
Private Context mContext;
Private int [] colors = new int [] {0xff626569, 0xff4f5257 };
Public MyListAdapter (Context context ){
MContext = context;
}
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return mListText. length;
}
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
ImageView image = null;
TextView title = null;
TextView content = null;
If (convertView = null ){
ConvertView = LayoutInflater. from (mContext). inflate (R. layout. colorlist, null );
Image = (ImageView) convertView. findViewById (R. id. color_image );
Title = (TextView) convertView. findViewById (R. id. color_title );
Content = (TextView) convertView. findViewById (R. id. color_text );
}
Int colorPos = position % colors. length;
ConvertView. setBackgroundColor (colors [colorPos]);
Title. setText (mListTitle [position]);
Content. setText (mListText [position]);
Image. setImageResource (R. drawable. portrait );
Return convertView;
}
}
}
The BaseAdapter makes the layout more complex. You only need to set the layout format in the xml file and extract the corresponding values in getView. See the following results:
There are also some built-in adapters, such as the SpinnerAdapter and SimpleCursorAdapter, which are relatively simple. You can take a look at the API to practice it yourself. Here is a special description, it is best to directly put the data retrieved from the database into SimpleCursorAdapter for convenience.
From the wangkuifeng0118 Column