Column View-listview

Source: Internet
Author: User

If a program needs to display an indefinite amount of data or a dynamically changing data, such as a contact list, a photo album, using the layout of the previous section to implement it will be very inflexible, the most effective view in this scenario is the list view (ListView or GridView). Both the ListView and the GridView inherit from Abslistview, so there are similar places in use. Here's how these two views are used.

The ListView is often used to show a series of similar types of data, with a simplified list of contacts explaining the basic usage of the ListView.

The main interface layout.

Main.xml

1. <relativelayout

Xmlns:android= "Http://schemas.android.com/apk/res/android"

2. android:id= "@+id/r1"

3. Android:layout_width= "Fill_parent"

4. android:layout_height= "Fill_parent" >

5. <textview

6. android:id= "@+id/t1"

7. Android:layout_width= "Wrap_content"

8. android:layout_height= "Wrap_content"

9. Android:layout_alignparenttop= "true"

Ten. Android:layout_centerhorizontal= "true"

Android:background= "#aabbaa"

android:text= "Clicked Contacts"/>

<listview.

Android:id= "@+id/list"

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

android:layout_below= "@id/t1"/>

</RelativeLayout>

The main interface has a relative layout, contains 2 controls, a textview,id is T1 at the top, and the ListView ID is a listview located below T1. T1 is used to display relevant content when we click on item in the ListView.

The layout interface for each item in the ListView is as follows.

Item.xml

1. <?xml version= "1.0" encoding= "Utf-8"?>

2. <relativelayout

3. xmlns:android= "Http://schemas.android.com/apk/res/android"

4. Android:layout_width= "Fill_parent"

5. android:layout_height= "Wrap_content"

6. android:paddingleft= "10dip" >

7. <imageview

8. android:id= "@+id/itemimage"

9. Android:layout_width= "Wrap_content"

android:layout_height= "Fill_parent"/>

<textview.

Android:id= "@+id/itemtitle"

android:text= "Name"

android:layout_height= "Wrap_content"

Android:layout_width= "Fill_parent"

android:layout_torightof= "@+id/itemimage"

Android:textsize= "24dip"/>

<textview.

android:text= "Tel:"

android:layout_height= "Wrap_content"

Android:layout_width= "Fill_parent"

Android:id= "@+id/itemtext"

android:layout_torightof= "@+id/itemimage"

android:layout_below= "@+id/itemtitle"/>

</RelativeLayout>

Item also has a relative layout, where line 6th android:paddingleft= "10dip" defines the contents of the item display with a blank area of 10 pixels left of the screen. Here Itemimage is used to display the avatar, the right side of the itemtitle used to display the name, Itemtext below the itemtitle used to display the phone number.

The last is Java source code.

Mylistview.java

1. Import android.app.Activity;

2. Import Android.content.Context;

3. Import Android.os.Bundle;

4. Import Android.view.LayoutInflater;

5. Import Android.view.View;

6. Import Android.view.ViewGroup; Import Android.widget.AdapterView;

7. Import Android.widget.BaseAdapter;

8. Import Android.widget.ImageView;

9. Import Android.widget.ListView;

. import Android.widget.TextView;

public class Mylistview extends Activity {

ListView ListView;

TextView Showinfo;

String[] titles={"Zhao 1", "Qian 2", "Zhang San", "John Doe", "Harry"};

String[]

texts={"13910000000", "13910000001", "13910000002", "13910000003", "13910000004"};

Buf=r.drawable.ic_launcher int;

Int[] Resids={buf,buf,buf,buf,buf};

@Override.

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

listview= (ListView) This.findviewbyid (r.id.list);

showinfo= (TextView) This.findviewbyid (R.ID.T1);

Listview.setadapter (New Myadapter (Titles,texts,resids));

Listview.setonitemclicklistener (New Adapterview.onitemclicklistener () {

@Override

Onitemclick public void (adapterview<?> arg0, View arg1, int arg2, long arg3) {

TextView title= (TextView) Arg1.findviewbyid (r.id.itemtitle);

. String info= "clicked contact is:" +title.gettext ();

TextView Text = (TextView) Arg1.findviewbyid (R.id.itemtext);

Info=info+ "\ n Tel:" +text.gettext ();

Showinfo.settext (info);}

33.});}

public class Myadapter extends Baseadapter {

String[] Itemtitles, itemtexts;

Int[] Itemimageres;

PNS public Myadapter (string[] itemtitles,string[] itemtexts,int[] itemimageres)

38. {

This.itemtitles=itemtitles;

This.itemtexts =itemtexts;

This.itemimageres=itemimageres;

42.}

. public int GetCount () {

. return itemtitles.length;

45.}

. public Object GetItem (int position) {

. return itemtitles[position];

48.}

. public long Getitemid (int position) {

. return position;

51.}

Public View GetView (int position, view Convertview, ViewGroup parent) {

if (Convertview = = null)

54. {

Layoutinflater Inflater =

(Layoutinflater) MyListView.this.getSystemService (Context.layout_inflater_service);

. View Itemview = inflater.inflate (R.layout.item, NULL);

TextView title = (TextView) Itemview.findviewbyid (r.id.itemtitle);

Title.settext (Itemtitles[position]);

TextView Text = (TextView) Itemview.findviewbyid (R.id.itemtext);

Text.settext (Itemtexts[position]);

ImageView image = (ImageView) Itemview.findviewbyid (r.id.itemimage);

Image.setimageresource (Itemimageres[position]);

. return Itemview;

else{.}

TextView title = (TextView) Convertview.findviewbyid (r.id.itemtitle);

Title.settext (Itemtitles[position]);

TextView Text = (TextView) Convertview.findviewbyid (R.id.itemtext);

Text.settext (Itemtexts[position]);

ImageView image = (ImageView) Convertview.findviewbyid (r.id.itemimage);

Image.setimageresource (Itemimageres[position]);

. return Convertview;

72.}}}

73.}

Line 14th to 17th we define the initialization data, the titles string array is the name information, the texts string array is the mobile phone number information, the Resids integer array is the Avatar resource drawable the avatar image ID value. In a formal project, data sources can be retrieved dynamically from a database, file, or online location.

Line 22nd to 23rd we get the ListView object and the TextView object defined in the main interface Main.xml file.

Line 24th We set the custom adapter Myadapter object for the ListView, which is defined in line 34th to 71st.

Line 25th to 32nd we set the ListView item to click Listener New Adapterview.onitemclicklistener (). The Onitemclick function of this listener is called when the ListView item is clicked. The parameter arg1 is the view of the item that was clicked, so we can get the Itemtitle and itemtext information from the arg1 and display this part of the information on the TextView at the top of the main interface.

The myadapter of line 34th to 71st inherits from Baseadapter, where itemview and what needs to be displayed are defined.

Line 37th to 42nd is the Myadapter constructor, and we save the incoming itemtitles, itemtexts, and Itemimageres to the object locally.

The following lines GetCount (), getItem (int position), and getitemid (int position) are used by the ListView to get information about item.

The 52nd to 71st Line of GetView (int position, view Convertview, ViewGroup parent) function is called by the ListView to get the specific View of each item. The ListView gets the number of item to display by GetCount () and then calls GetView one by one to get the view and display it. The position parameter specifies which Item,convertview object is required to tell adapter if there is an item view that is not displayed on the screen after it has been removed from the screen, and Convertview is an old view. In this case, it is not necessary to construct 10,000 item view objects because of 10,000 records.

Lines 53rd and 64th of the IF (Convertview = = null) are for the sake of the old Itemview, if the Convertview exists directly to the old and Convertview Itemtitle, Itemtext and Itemimage are updated and returned to the ListView for use.

Lines 55th and 56th layoutinflater the function of an object is to associate and instantiate a View object with an XML layout file. Here we associate and instantiate Itemview and Item.xml.

Line 57th to 62nd Itemview Object Instantiation, find the Itemtitle, Itemtext, and itemimage in the layout file through Findviewbyid (), and the data in the data source itemtitles[position], Itemtexts[position] and itemimageres[position] are assigned values.

The 63rd row returns Itemview to the ListView.

The running effect is shown.

Column View-listview

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.