Commonly used Android components: ListView and androidlistview
Use ListAdapter as the ListView adapter.
Use the ListView. setAdapter () method to set the ListView adapter.
|
Adapter =NewArrayAdapter <String> (This, Android. R. layout.Simple_list_item_1);
Adapter. add ("hello ");
Adapter. add ("eoe ");
Lv. setAdapter (adapter ); |
Android in front of R indicates that resources provided by the system are used.
Create an XML file under the layout package with the following content:
|
<? Xmlversion ="1.0"Encoding ="UTF-8"?>
<ListViewxmlns: android =Http://schemas.android.com/apk/res/android"
Android: layout_width ="Match_parent"
Android: layout_height ="Match_parent">
</ListView> |
Create an Adapter as follows:
|
Adapter =NewArrayAdapter <String> (This, R. layout.List_cell_view);
Adapter. add ("hello ");
Adapter. add ("eoe ");
Lv. setAdapter (adapter ); |
|
PrivateBaseAdapter adapter =NewBaseAdapter (){
PrivateString [] data = {"user1", "user2", "user3", "user4", "user5", "user6 ",
"User7", "user8", "user9", "user10", "user11", "user12", "user13", "user14 ",};
@ Override
PublicView getView (IntPosition, View convertView, ViewGroup parent ){
TextView TV =NewTextView (MainActivity.This);
TV. setText (getItem (position ));
ReturnTV;
}
@ Override
PublicLongGetItemId (IntPosition ){
ReturnPosition;
}
@ Override
PublicString getItem (IntPosition ){
ReturnData [position];
}
@ Override
PublicIntGetCount (){
ReturnData. length;
}
}; |
The number of list items displayed on the interface when the program is running. The getView method is how many times the program is executed.
The View in the getView parameter indicates the View that is recycled by the system.
Optimization:
|
TextView TV =Null;
If(ConvertView! =Null){
TV = (TextView) convertView;
}Else{
TV =NewTextView (MainActivity.This);
}
TV. setText (getItem (position )); |
Create a new Linearlayout layout file in layout:
|
<? Xmlversion ="1.0"Encoding ="UTF-8"?>
<LinearLayoutxmlns: android =Http://schemas.android.com/apk/res/android"
Android: layout_width ="Match_parent"
Android: layout_height ="Match_parent"
Android: orientation ="Horizontal">
<ImageView
Android: layout_width ="120dp"
Android: layout_height ="120dp"
Android: id ="@ + Id/icon"/>
<LinearLayout
Android: layout_weight ="1"
Android: layout_width ="0dp"
Android: layout_height ="Match_parent"
Android: orientation ="Vertical">
<TextView
Android: id ="@ + Id/name"
Android: layout_width ="Wrap_content"
Android: layout_height ="Wrap_content"/>
<TextView
Android: id ="@ + Id/dec"
Android: layout_width ="Wrap_content"
Android: layout_height ="Wrap_content"/>
</LinearLayout>
</LinearLayout> |
Create an Adapter:
|
PrivateBaseAdapter adapter =NewBaseAdapter (){
PrivateListCellData [] data =NewListCellData [] {
NewListCellData ("James", "male", android. R. drawable.Ic_btn_speak_now),
NewListCellData ("Lili", "female", android. R. drawable.Ic_dialog_dialer),
};
@ Override
PublicView getView (IntPosition, View convertView, ViewGroup parent ){
LinearLayout line =Null;
If(ConvertView! =Null){
Line = (LinearLayout) convertView;
}Else{
Line = (LinearLayout) LayoutInflater.From(MainActivity.This). Inflate (R. layout.List_cell_view,Null);
}
ListCellData item = getItem (position );
ImageView image = (ImageView) line. findViewById (R. id.Icon);
TextView name = (TextView) line. findViewById (R. id.Name);
TextView dec = (TextView) line. findViewById (R. id.Dec);
Image. setImageResource (item. getIcon ());
Name. setText (item. getUsername ());
Dec. setText (item. getSex ());
ReturnLine;
}
@ Override
PublicLongGetItemId (IntPosition ){
ReturnPosition;
}
@ Override
PublicListCellData getItem (IntPosition ){
ReturnData [position];
}
@ Override
PublicIntGetCount (){
ReturnData. length;
}
}; |