Commonly used Android components: ListView and androidlistview

Source: Internet
Author: User

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;

}

};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.