Android Development most common examples of finishing----(3) Customizing the ListView (Custom Baseadapter Implementation)

Source: Internet
Author: User

In the previous tutorial, we implemented a custom ListView using Simpleadapter, but it was inconvenient to have a different style setting for each of the ListView item options, which would be cumbersome, Because the Simpleadapter uses a uniform style style. If you want to implement different item styles using different styles, then you need to integrate baseadapter to implement them.

First, ActivityMainactivity.java Source:


public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//step1string[] Contentarray = new String[]{"Content 1", "Content 2 "," Content 3 "," Content 4 "," Content 5 "}; ListView LV = (ListView) Findviewbyid (r.id.lv);//step2arraylist

Second, XML layout file
(1) Mainactivity main interface layout source code activity_main.xml:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent "    android:orientation=" vertical ">    <listview    Android:id= "@+id/lv" android:layout_width= "match_parent"     android:layout_height= "Wrap_content"/></ Linearlayout>
(2) The layout of the ListView List Unit source code list_item.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:layout_height=" Match_parent "android:layout_width=" match_parent "android:gravity=" Center_ Vertical "android:orientation=" horizontal "> <linearlayout android:layout_width=" 0DP "Android:lay out_height= "Wrap_content" android:layout_weight= "2" android:gravity= "center_vertical" android:orientation= " Vertical "> <textview android:id=" @+id/starttimetext "android:layout_height=" Wrap_              Content "android:layout_width=" match_parent "/> <textview android:id=" @+id/endtimetext " android:layout_height= "Wrap_content" android:layout_width= "match_parent"/> </LinearLayout> < ImageView android:id= "@+id/separator" android:layout_weight= "1" android:layout_width= "0DP" android:layout_height = "Wrap_content"/> <textview android:id= "@+id/content"     Android:layout_width= "0DP" android:layout_height= "wrap_content" android:layout_weight= "/> </LinearLayout" >

Iii. Related Resources

Files under res/drawable-mdpi/:


Separator1.png separator2.png separator3.png separator4.png separator5.png


Four, the effect



Android Development most common examples of finishing----(3) Customizing the ListView (Custom Baseadapter Implementation)

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.