Listview is a list view that displays the controls provided by listadapter in a vertical and scrollable list. Note that you need to create an adapter and set it to listview.
1. arrayadapter
Arrayadapter is constructed by three parameters. The first parameter is context, the second parameter is the layout defined in the r file, and the third parameter is an array, there is no limit on the type of each item in the array.
The default Layout mode can be defined by Android. R. layout. XX.
Private Static string [] DATA = {"A", "B", "C", "D "};
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Setcontentview (R. layout. Main );
Listview = new listview (this );
Arrayadapter adapter = newArrayadapter <string> (this, Android. R. layout. simple_list_item_1, data );
Listview. setadapter (adapter );
Setcontentview (listview );
}
If the style arraylayout. xml of each textview in the Custom listview is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Textview xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"
Android: gravity = "center_horizontal"/>
In activity, specify the second parameter of arrayadapter as arraylayout. xml:
Private Static string [] DATA = {"A", "B", "C", "D "};
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Setcontentview (R. layout. Main );
Listview = new listview (this );
Arrayadapter adapter = new arrayadapter <string> (this,R. layout. arraylayout, Data );
Listview. setadapter (adapter );
Setcontentview (listview );
}
2 simpleadapter
Each item in the arraylist of simpleadapter is a map <string,?> Type. Each map object corresponds to one item in listv.
Private listview;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Listview = new listview (this );
Data2 = new arraylist <Map <string, Object> ();
Map <string, Object> item;
Item = new hashmap <string, Object> ();
Item. Put ("name", "Zhang San ");
Item. Put ("gender", "male ");
Item. Put ("Age", "25 ");
Data2.add (item );
Item = new hashmap <string, Object> ();
Item. Put ("name", "Li Si ");
Item. Put ("gender", "male ");
Item. Put ("Age", "33 ");
Data2.add (item );
Item = new hashmap <string, Object> ();
Item. Put ("name", "John ");
Item. Put ("gender", "female ");
Item. Put ("Age", "31 ");
Data2.add (item );
Simpleadapter adapter = New simpleadapter (this, data2,
R. layout. simplelayout, new string [] {"name", "gender", "Age"}, new int [] {
R. Id. tv01, R. Id. tv02, R. Id. tv03 });
Listview. setadapter (adapter );
Setcontentview (listview );
The layout file for each item in listview is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"
Android: Orientation = "horizontal">
<Textview Android: Id = "@ + ID/tv01" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: width = "150dp"/>
<Textview Android: Id = "@ + ID/tv02" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: width = "150dp"/>
<Textview Android: Id = "@ + ID/tv03" Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" Android: width = "150dp"/>
</Linearlayout>
If the layout file of the activity is set to contain not only listview, as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Imageview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"
Android: src = "@ drawable/img01"/>
<ListviewAndroid: Id = "@ + ID/listview01" Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" Android: choicemode = "singlechoice"/>
</Linearlayout>
In activity:
Setcontentview (R. layout. Main );
Listview = (listview) findviewbyid (R. Id. listview01 );
Listview. setadapter (adapter );
3 baseadapter
Public class mainactivity extends activity {
/** Called when the activity is first created .*/
Int [] Drawableids = {R. drawable. img01, R. drawable. img02, R. drawable. img03 };
Int [] Msgids = {R. String. str1, R. String. str2, R. String. str3 };
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Listview Listview = (Listview) findviewbyid (R. Id. listview01 );
Baseadapter BA = new Baseadapter (){
@ Override
Public View Getview (INT position, view convertview, viewgroup parent ){
// Todo auto-generated method stub
Linearlayout LL = new linearlayout (mainactivity. This );
Ll. setorientation (linearlayout. Horizontal );
Ll. setpadding (5, 5, 5, 5 );
Imageview II = new imageview (mainactivity. This );
Ii. setimagedrawable (getresources (). getdrawable (drawableids [position]);
Ii. setscaletype (imageview. scaletype. fit_xy );
Ii. setlayoutparams (new gallery. layoutparams (50, 50 ));
Ll. addview (II );
Textview TV = new textview (mainactivity. This );
TV. settext (getresources (). gettext (msgids [position]);
TV. settextsize (24 );
TV. settextcolor (mainactivity. This. getresources (). getcolor (R. color. White ));
TV. setpadding (5, 5, 5, 5 );
TV. setgravity (gravity. Left );
Ll. addview (TV );
Return Ll ;
}
@ Override
Public long getitemid (INT position ){
// Todo auto-generated method stub
Return 0;
}
@ Override
Public object getitem (INT position ){
// Todo auto-generated method stub
Return NULL;
}
@ Override
Public int getcount (){
// Todo auto-generated method stub
Return 3;
}
};
Listview. setadapter (BA );
}
}
4 listactivity
If listactivity is used, the listview in the activity is full of screens.
In the layout file, you must define a listview with the ID @ ID/Android: List. The other one must be defined but not the textview with the ID @ ID/Android: empty, it is the content displayed when there is no data in the listview.
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" Android: layout_height = "fill_parent">
<Listview Android: Id = "@ ID/Android: List" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
<Textview Android: Id = "@ ID/Android: empty" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent" Android: text = "no data"/>
</Linearlayout>
In activity, each row of listview is set as in the previous method.
String [] DATA = {"A", "B", "C", "D "};
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Setlistadapter (New arrayadapter <string> (this, Android. R. layout. simple_list_item_1, data ));
}