Android Learning--the use of control listview

Source: Internet
Author: User
Tags pear

I. Simple usage of the ListView

Start by creating a new Listviewtest project and have Android studio automatically create a good activity. Then modify the code in the Activity_main.xml as follows:

1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <linearlayout xmlns:android= "/http Schemas.android.com/apk/res/android " 3     android:layout_width=" Match_parent " 4     android: layout_height= "Match_parent" > 5  6    <ListView  7        android:id= "@+ Id/list_view " 8        android:layout_width=" match_parent " 9        android:layout_height=" Match_parent ">    </ListView> </LinearLayout>

Next, modify the code in Mainactivity:

1  Public classMainactivityextendsappcompatactivity {2 3     PrivateString[] data={"Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango", "Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango"};4 @Override5     protected voidonCreate (Bundle savedinstancestate) {6         Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.activity_main);8 9Arrayadapter<string> adapter=NewArrayadapter<string> (mainactivity. This, Android. R.layout.simple_list_item_1,data);TenListView listview=(ListView) Findviewbyid (R.id.list_view); One Listview.setadapter (adapter); A     } -}

The data in the array cannot be passed directly to the ListView and needs to be implemented with an adapter.

The Arrayadapter constructor, in turn, passes in the current context, the ID of the ListView Child layout, and the data to be adapted;

Call the Setadapter () method of the ListView to pass the built-in adapter object so that the correlation between the ListView and the data is complete.

Run the program, you can scroll to see the data outside the screen.

Ii. Customizing the ListView Interface

First prepare a set of pictures, respectively, corresponding to each of the fruits provided above (note the picture size as far as possible), placed in the drawable directory, note that the name can not appear in uppercase letters (such as Apple illegal);

Create a new. java file under Com.example.administrator.listviewtest, define an entity class fruit, as an adaptation type for the ListView adapter.

1  Public classfruit{2     PrivateString name;3     Private intimageId;4      PublicFruit (String name,intimageId) {5          This. name=name;6          This. imageid=imageId;7     }8      PublicString GetName () {9         returnname;Ten     } One      Public intGetimageid () { A         returnimageId; -     } -}

Then specify a custom layout for the child of the ListView and create a new fruit_item.xml in the layout directory.

1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Match_parent"4android:layout_height= "Wrap_content" >5 6<ImageView7Android:id= "@+id/fruit_image"8Android:layout_width= "Wrap_content"9android:layout_height= "Wrap_content"/>Ten  One<TextView AAndroid:id= "@+id/fruit_name" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" theAndroid:layout_gravity= "Center_vertical" -android:layout_marginleft= "10DP" -/> -</LinearLayout>

Next, create a custom adapter that also creates a new. java file under Com.example.administrator.listviewtest, overriding a set of constructors for the parent class to pass in the ID and data of the context, ListView child layout , named Fruitadapter, the code is as follows:

1  Public classFruitadapterextendsArrayadapter<fruit> {2     Private intresourceId;3      PublicFruitadapter (Context context,intTextviewresourceid, list<fruit>objects) {4         Super(context,textviewresourceid,objects);5Resourceid=Textviewresourceid;6     }7 @Override8      PublicView GetView (intPosition,view Convertview,viewgroup Parent) {9Fruit Fruit=getitem (position);//gets an instance of the current itemTenView view= Layoutinflater.from (GetContext ()). Inflate (Resourceid,parent,false); OneImageView fruitimage=(ImageView) View.findviewbyid (r.id.fruit_image); ATextView fruitname=(TextView) View.findviewbyid (r.id.fruit_name); - Fruitimage.setimageresource (Fruit.getimageid ()); - Fruitname.settext (Fruit.getname ()); the         returnview; -     } -}

The following changes the code in Mainactivity:

1  Packagecom.example.administrator.listviewtest;2 3 Importandroid.support.v7.app.AppCompatActivity;4 ImportAndroid.os.Bundle;5 ImportAndroid.widget.ArrayAdapter;6 ImportAndroid.widget.ListView;7 8 Importjava.util.ArrayList;9 Importjava.util.List;Ten  One  Public classMainactivityextendsappcompatactivity { A     PrivateList<fruit> fruitlist=NewArraylist<>(); -  - //private string[] data={"Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", " Cherry "," Mango "," Apple "," Banana "," Orange "," Watermelon "," Pear "," Grape "," Pineapple "," Strawberry "," Cherry "," Mango "}; the @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); + initfruits (); Initializing fruit Data -Fruitadapter adapter=NewFruitadapter (mainactivity. This, r.layout.fruit_item,fruitlist); +  A //arrayadapter<string> adapter = new Arrayadapter<string> (Mainactivity.this, Android. R.layout.simple_list_item_1, data); atListView ListView =(ListView) Findviewbyid (R.id.list_view); - Listview.setadapter (adapter); -     } -  -     Private voidinitfruits () { -          for(inti=0;i<2;i++){ inFruit apple=NewFruit ("Apple", r.drawable.apple); - Fruitlist.add (apple); toFruit orange=NewFruit ("Orange", r.drawable.orange); + Fruitlist.add (orange); -Fruit banana=NewFruit ("Banana", R.drawable.banana); the Fruitlist.add (banana); *Fruit waterlenmo=NewFruit ("Waterlemon", R.drawable.waterlemon); $ Fruitlist.add (WATERLENMO);Panax NotoginsengFruit pear=NewFruit ("Pear", r.drawable.pear); - fruitlist.add (pear); theFruit grape=NewFruit ("Grape", r.drawable.grape); + Fruitlist.add (grape); AFruit pineapple=NewFruit ("Pineapple", r.drawable.pineapple); the Fruitlist.add (pineapple); +Fruit strawberry=NewFruit ("Strawberry", R.drawable.straw); - Fruitlist.add (strawberry); $Fruit cherry=NewFruit ("Cherry", R.drawable.cherry); $ Fruitlist.add (cherry); -Fruit mango=NewFruit ("Mango", R.drawable.mango); - Fruitlist.add (mango); the         } -     }Wuyi}

After running the program, the following (image size later realized, too lazy to change ...) ):

Android Learning--the use of control listview

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.