Step 7: listview (2)

Source: Internet
Author: User

This article from http://blog.csdn.net/hellogv/

 

In the sixth article of getting started with Android, listview (1) describes how to create a custom control with two lines of text and use it as an item of listview. Next, we will introduce the usage of listview and item in a more in-depth manner.

First, let's take a look at the code running result of this article. The item in this article is more left-side icon than the item in the previous article:


 


The source code of Main. XML is the same as that in the previous article. I will not explain it here. The code for item. XML is directly pasted, that is, it implements the imageitem UI:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <relativelayout <br/> Android: layout_width = "fill_parent" <br/> xmns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_height = "wrap_content" <br/> Android: paddingbottom = "4dip" <br/> Android: paddingleft = "12dip"> <br/> <imageview <br/> Android: layout_width = "wrap_content" <br/> Android: Id = "@ + ID/itemimage" Android: layout_height = "fill_parent"> <br/> </imageview> <br/> <textview <br/> Android: text = "textview01" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" <br/> Android: Id = "@ + ID/itemtitle" Android: layout_torightof = "@ + ID/itemimage" Android: textsize = "20dip"> <br/> </textview> <br/> <textview <br/> Android: TEXT = "textview02" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_width = "fill_parent" <br/> Android: id = "@ + ID/itemtext" Android: layout_torightof = "@ + ID/itemimage" Android: layout_below = "@ + ID/itemtitle"> <br/> </textview> <br/> </relativelayout>

Explain the code of item. xml: The relativelayout layout is used here. The key attributes of the control are:

Itemtitle attribute Android: layout_torightof = "@ + ID/itemimage", where itemtitle is on the right of itemimage;

Itemtext attributes: Android: layout_torightof = "@ + ID/itemimage", itemtext on the Right of itemimage, Android: layout_below = "@ + ID/itemtitle", and itemtext under itemtitle.

Finally, paste the source code of Java, with the focus on layoutinflate usage. Layoutinflate is used as follows:

  • Layoutinflater is used to associate and instantiate a view object with an XML layout file.
  • After the view object is instantiated, you can use findviewbyid () to find the component with the specified ID in the layout file.

 Package COM. testlistview; </P> <p> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. OS. bundle; <br/> Import android. view. layoutinflater; <br/> Import android. view. view; <br/> Import android. view. viewgroup; <br/> Import android. widget. baseadapter; <br/> Import android. widget. imageview; <br/> Import android. widget. listview; <br/> Import android. widget. textview; </P> <p> public class testlistview extends activity {<br/> listview; <br/> string [] titles = {"Title 1 ", "title 2", "Title 3", "Title 4" };< br/> string [] texts = {"text content a", "text content B ", "text content C", "text content D" };< br/> int [] resids = {R. drawable. icon, R. drawable. icon, R. drawable. icon, R. drawable. icon}; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> This. settitle ("baseadapter for listview"); <br/> listview = (listview) This. findviewbyid (R. id. listview1); <br/> listview. setadapter (New listviewadapter (titles, texts, resids); <br/>}</P> <p> public class listviewadapter extends baseadapter {<br/> View [] itemviews; </P> <p> Public listviewadapter (string [] itemtitles, string [] itemtexts, <br/> int [] itemimageres) {<br/> itemviews = new view [itemtitles. length]; </P> <p> for (INT I = 0; I <itemviews. length; I ++) {<br/> itemviews [I] = makeitemview (itemtitles [I], itemtexts [I], <br/> itemimageres [I]); <br/>}</P> <p> Public int getcount () {<br/> return itemviews. length; <br/>}</P> <p> Public View getitem (INT position) {<br/> return itemviews [position]; <br/>}</P> <p> Public long getitemid (INT position) {<br/> return position; <br/>}</P> <p> private view makeitemview (string strtitle, string strtext, int resid) {<br/> layoutinflater Inflater = (layoutinflater) testlistview. this <br/>. getsystemservice (context. layout_inflater_service); </P> <p> // use the itemview and R. layout. item Association <br/> View itemview = Inflater. inflate (R. layout. item, null); </P> <p> // instance R through the findviewbyid () method. layout. <br/> textview Title = (textview) itemview. findviewbyid (R. id. itemtitle); <br/> title. settext (strtitle); <br/> textview text = (textview) itemview. findviewbyid (R. id. itemtext); <br/> text. settext (strtext); <br/> imageview image = (imageview) itemview. findviewbyid (R. id. itemimage); <br/> image. setimageresource (resid); </P> <p> return itemview; <br/>}</P> <p> Public View getview (INT position, view convertview, viewgroup parent) {<br/> If (convertview = NULL) <br/> return itemviews [position]; <br/> return convertview; <br/>}</P> <p>}

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.