Android learning-use of the control ListView, androidlistview

Source: Internet
Author: User

Android learning-use of the control ListView, androidlistview

I. Simple usage of ListView

First, create a ListViewTest project and let Android Studio automatically create the activity. Then modify the code in 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">10    </ListView>11 </LinearLayout>

Next, modify the code in MainActivity:

 1 public class MainActivity extends AppCompatActivity { 2  3     private String[] data={"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango","Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango"}; 4     @Override 5     protected void onCreate(Bundle savedInstanceState) { 6         super.onCreate(savedInstanceState); 7         setContentView(R.layout.activity_main); 8  9         ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);10         ListView listview=(ListView)findViewById(R.id.list_view);11         listview.setAdapter(adapter);12     }13 }

The data in the array cannot be directly transmitted to the ListView, which must be implemented by using an adapter.

In the constructor of ArrayAdapter, input the current context, the id of the ListView subitem layout, and the data to be adapted in sequence;

Call the setAdapter () method of ListView to pass the constructed adapter object in, so that the association between ListView and data is established.

Run the program. You can scroll to view data outside the screen.

Ii. Customize the ListView Interface

First, prepare a group of images corresponding to each fruit provided above (note that the image size should be as consistent as possible) and put them in the drawable directory, note that the name cannot contain uppercase letters (for example, Apple is invalid );

Create a. java file under com. example. administrator. listviewtest and define an Object Class Fruit as the adaptation type of the ListView adapter.

 1 public class Fruit{ 2     private String name; 3     private int imageId; 4     public Fruit(String name,int imageId){ 5         this.name=name; 6         this.imageId=imageId; 7     } 8     public String getName(){ 9         return name;10     }11     public int getImageId(){12         return imageId;13     }14 }

Specify a custom layout for the subitem of ListView and create 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" 3     android:layout_width="match_parent" 4     android:layout_height="wrap_content"> 5  6     <ImageView 7         android:id="@+id/fruit_image" 8         android:layout_width="wrap_content" 9         android:layout_height="wrap_content" />10 11     <TextView12         android:id="@+id/fruit_name"13         android:layout_width="wrap_content"14         android:layout_height="wrap_content"15         android:layout_gravity="center_vertical"16         android:layout_marginLeft="10dp"17         />18 </LinearLayout>

Next, create a custom adapter. example. administrator. create in listviewtest. java file, override a group of constructors of the parent class, used to pass in the context, ListView subitem layout id and data, named as FruitAdapter, the Code is as follows:

1 public class FruitAdapter extends ArrayAdapter <Fruit> {2 private int resourceId; 3 public FruitAdapter (Context context, int textViewResourceId, List <Fruit> objects) {4 super (context, textViewResourceId, objects); 5 resourceId = textViewResourceId; 6} 7 @ Override 8 public View getView (int position, View convertView, ViewGroup parent) {9 Fruit fruit = getItem (position ); // obtain the instance 10 View = LayoutInflater of the current item. from (getContext ()). inflate (resourceId, parent, false); 11 ImageView fruitImage = (ImageView) view. findViewById (R. id. fruit_image); 12 TextView fruitName = (TextView) view. findViewById (R. id. fruit_name); 13 fruitImage. setImageResource (fruit. getImageId (); 14 fruitName. setText (fruit. getName (); 15 return view; 16} 17}

Modify the MainActivity Code as follows:

1 package com. example. administrator. listviewtest; 2 3 import android. support. v7.app. appCompatActivity; 4 import android. OS. bundle; 5 import android. widget. arrayAdapter; 6 import android. widget. listView; 7 8 import java. util. arrayList; 9 import java. util. list; 10 11 public class MainActivity extends AppCompatActivity {12 private List <Fruit> fruitList = new ArrayList <> (); 13 14 // private String [] data = {"Apple ", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango", "Apple ", "Banana", "Orange", "Watermelon", "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango "}; 15 @ Override16 protected void onCreate (Bundle savedInstanceState) {17 super. onCreate (savedInstanceState); 18 setContentView (R. layout. activity_main); 19 initFruits (); // initialize the fruit data 20 FruitAdapter adapter = new FruitAdapter (MainActivity. this, R. layout. fruit_item, fruitList); 21 22 // ArrayAdapter <String> adapter = new ArrayAdapter <String> (MainActivity. this, android. r. layout. simple_list_item_1, data); 23 ListView listview = (ListView) findViewById (R. id. list_view); 24 listview. setAdapter (adapter); 25} 26 27 private void initFruits () {28 for (int I = 0; I <2; I ++) {29 Fruit apple = new Fruit ("Apple", R. drawable. apple); 30 fruitList. add (apple); 31 Fruit orange = new Fruit ("Orange", R. drawable. orange); 32 fruitList. add (orange); 33 Fruit banana = new Fruit ("Banana", R. drawable. banana); 34 fruitList. add (banana); 35 Fruit waterlenmo = new Fruit ("Waterlemon", R. drawable. waterlemon); 36 fruitList. add (waterlenmo); 37 Fruit pear = new Fruit ("Pear", R. drawable. pear); 38 fruitList. add (pear); 39 Fruit grape = new Fruit ("Grape", R. drawable. grape); 40 fruitList. add (grape); 41 Fruit pineapple = new Fruit ("Pineapple", R. drawable. pineapple); 42 fruitList. add (pineapple); 43 Fruit strawberry = new Fruit ("Strawberry", R. drawable. straw); 44 fruitList. add (strawberry); 45 Fruit cherry = new Fruit ("Cherry", R. drawable. cherry); 46 fruitList. add (cherry); 47 Fruit mango = new Fruit ("mango", R. drawable. mango); 48 fruitList. add (mango); 49} 50} 51}

After running the program, as shown below (the image size was later realized, too lazy to change ......) :

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.