Android: Daily Learning Notes (8) ——— Explore UI Development (5)

Source: Internet
Author: User
Tags pear

Android: Daily Learning Notes (8) ——— Explore UI Development (5) Simple use of ListView control
 Public classMainactivityextendsappcompatactivity {PrivateString[] data={"Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry",
"Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry",
"Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry"}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Arrayadapter<String> adapter =NewArrayadapter<string>
(Mainactivity. This, R.layout.support_simple_spinner_dropdown_item,data); ListView ListView=(ListView) Findviewbyid (R.id.list_view); Listview.setadapter (adapter); }}

Description

ARRAYADAPTER Specifies the data type to be adapted by generics, and then in the constructor the current context, the ID of the ListView Child layout, and the data to be adapted are passed in.

We used the built-in layout file for Android here.

The ListView sets the adapter, Setadapter (), and passes the built-in adapter object in order to establish the relationship between the data and the ListView.

Customizing the ListView Interface

1. Defining an entity class

 Public classFruit {PrivateString name; Private intImageID;  PublicFruit (String name,intImageID) {         This. Name =name;  This. ImageID =ImageID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetimageid () {returnImageID; }     Public voidSetimageid (intImageID) {         This. ImageID =ImageID; }}

2. Customizing the Sub-item layout file

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <ImageViewAndroid:id= "@+id/fruit_img"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" />    <TextViewAndroid:id= "@+id/fruit_text"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_vertical"Android:layout_marginleft= "10DP"/></LinearLayout>

3. Writing an adaptation class

 Public classFruitadapterextendsArrayadapter<fruit> {    Private intResourceID;  PublicFruitadapter (@NonNull context context, @LayoutResintResource, @IdResintTextviewresourceid, @NonNull list<fruit>objects) {        Super(context, resource, Textviewresourceid, objects); ResourceID=Textviewresourceid; } @NonNull @Override PublicView GetView (intposition, @Nullable View Convertview, @NonNull viewgroup parent) {Fruit Fruit=GetItem (position); View View= Layoutinflater.from (GetContext ()). Inflate (Resourceid,parent,false); ImageView Fruitimage=(ImageView) View.findviewbyid (r.id.fruit_img); TextView Fruittext=(TextView) View.findviewbyid (R.id.fruit_text);        Fruitimage.setimageresource (Fruit.getimageid ());        Fruittext.settext (Fruit.getname ()); returnview; }}

4. Application

 Public classMainactivityextendsappcompatactivity {PrivateList<fruit> fruitlist=NewArraylist<>(); protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);Setcontentview (R.layout.activity_main);        Initfruits (); Fruitadapter Adapter=NewFruitadapter (mainactivity. This, r.layout.activity_main,r.layout.entry_fruit_item,fruitlist); ListView ListView=(ListView) Findviewbyid (R.id.list_view);    Listview.setadapter (adapter); }     Public voidinitfruits () { for(inti=0;i<100;i++) {Fruitlist.add (NewFruit ("XXX", r.drawable.next_24px)); }    }}

I

Android: Daily Learning Notes (8) ——— Explore UI Development (5)

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.