Android control: ListView 1

Source: Internet
Author: User

In android development, ListView is a commonly used component that displays specific content in the form of a list and can be automatically displayed based on the length of data.

Main. xml layout File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/MyListView">
</ListView>
</LinearLayout>

My_listitem.xml layout File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:id="@+id/myListItem"
android:paddingBottom="3dip"
android:paddingLeft="10dip">
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/itemTitle"
android:textSize="20dip">
</TextView>
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/itemText">
</TextView>
</LinearLayout>

LsActivity class

Package com. ljq. ls;

Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. List;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. ListView;
Import android. widget. SimpleAdapter;

Public class LsActivity extends Activity {
Private ListView list = null;

Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
List = (ListView) findViewById (R. id. MyListView );

// Organize data sources
List <HashMap <String, String> mylist = new ArrayList <HashMap <String, String> ();
For (int I = 0; I <10; I ++ ){
HashMap <String, String> map = new HashMap <String, String> ();
Map. put ("itemTitle", "This is Title ");
Map. put ("itemText", "This is text ");
Mylist. add (map );
}

// Configure the adapter
SimpleAdapter adapter = new SimpleAdapter (this,
Mylist, // Data Source
R. layout. my_listitem, // display layout
New String [] {"itemTitle", "itemText"}, // attribute field of the Data Source
New int [] {R. id. itemTitle, R. id. itemText}); // Control id in the layout
// Add and display
List. setAdapter (adapter );
}
}

Running result

Related Article

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.