A simple list application in Android

Source: Internet
Author: User

I thought using list in Android should be a simple task, but -- I am wrong! I have been reading books, followed by book examples.ProgramI learned to write a list, but I still have no skills. Today, I suddenly saw a video tutorial, and I felt a little confused. This video tutorial is www.mars-droid.com, beginners can download learning, or very good, painted color! Haha ~

Now, let's get started.

In the android program, the use of listview is relatively complex. You not only need to add a listview in the activity for the entire list, but also need a layout file, the layout file controls the display mode of each record (each row) in the listview. For example, a listview contains records of several rows, but each row has multiple fields. How to control these fields is what the layout file needs to handle.

1. Main activity layout:

In the main activity window, you only need to simply add a listview to the activity and set the attributes of the listview.

2. layout of each item in listview:

We use an XML layout file to control the layout of each item. For example, the following XML file places two textviews on each item.

Item. xml
 <  Linearlayout
Android: layout_height = "Fill_parent"
Android: layout_width = "Fill_parent"
Android: paddingbottom = "1dip"
Android: paddingleft = "10dip"
Android: paddingright = "10dip"
Android: paddingtop = "1dip" >
< Textview
Android: ID = "@ + ID/user_name"
Android: layout_width = "180dip"
Android: layout_height = "30dip"
Android: textsize = "10pt"
Android: singleline = "True" />
< Textview
Android: ID = "@ + ID/user_num"
Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent"
Android: gravity = "Right"
Android: textsize = "10pt" />
</ Linearlayout >

3. Java class of listview

Sets the primary activity to inherit from the listactivity class.

Store the display data of listview through an arraylist

Create a simpleadapter instance and bind the setlistadapter instance to the current listactivity.

Set the action to be performed when each item is clicked.

Myactivity. Java

Public     Class  Myactivity  Extends  Listactivity {
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. Id. Main );

Arraylist < Hashmap < String, string > List = New Arraylist < Hashmap < String, string > ();
Hashmap < String, string > Map1 = New Hashmap < String, string > ();
Hashmap < String, string > MAP2 = New Hashmap < String, string > ();
Hashmap < String, string > Map3 = New Hashmap < String, string > ();
Map1.put ( " User_name " , " Allen " );
Map1.put ( " User_num " , " 123 " );
Map2.put ( " User_name " , " Bobo " );
Map2.put ( " User_num " , " 456 " );
Map3.put ( " User_name " , " David " );
Map3.put ( " User_num " , " 789 " );
List. Add (map1 );
List. Add (MAP2 );
List. Add (map3 );

Simpleadapter listadapter = New Simpleadapter (
This , // Context
List, // Bound Data Source
R. layout. item, // Item layout File
New String [] { " User_name " , " User_num " }, // Column name of listview
New Int [] {R. Id. user_name, R. Id. user_num }); // Position of each widget in item

Setlistadapter (listadapter );
}

@ Override
Protected Void Onlistitemclick (listview L, view V, Int Position, Long ID ){
// Todo auto-generated method stub
Super . Onlistitemclick (L, V, position, ID );
}
}

This example is the most basic example program of listview. It can be better applied only by understanding the principle. As for the programCodeThe explanation, don't shift the door to make an axe, you are free to download the video inside the www.mars-droid.com to see, it explains in more detail. The file name is 201701_13_common control file (32.16.mp4)

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.