Because the mobile phone screen space is relatively limited, can be a one-time display on the screen is not much, when our program has
When a large amount of data needs to be displayed, it can be implemented with a ListView. The ListView allows the user to move up and down
Swipe to scroll the off-screen data to the screen, while the original data on the screen scrolls out of the screen .
Simple usage of 1.ListView
Start by creating a new Listviewtest project and have ADT automatically help us create a good activity. And then Modify
The code in Activity_main.xml is as follows:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<listview
Android:id= "@+id/list_view"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
</ListView>
</LinearLayout>
Adding a ListView control to a layout is fairly straightforward, specifying an ID for the ListView, and then the width
and height are set to match_parent, so that the ListView occupies the entire layout space.
Next, modify the code in Mainactivity as follows:
public class Mainactivity extends Activity {
Private string[] data = {"Apple", "Banana", "Orange", "Watermelon" ,
"Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango"};
@Override
protected void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Arrayadapter<string> adapter = new Arrayadapter<string> (
Mainactivity.this, Android. R.layout.simple_list_item_1, data);
ListView ListView = (ListView) Findviewbyid (R.id.list_view);
Listview.setadapter (adapter);
}
}
Since the ListView is for displaying a large amount of data, we should first provide the data well. The data can be
downloaded from the Web or read from the database, and should be determined by the application scenario. Here we
simply use a data array to test, which contains a lot of fruit names.
However, the data in the array cannot be passed directly to the ListView, and we need the adapter to do it.
There are many implementations of adapters available in Android, and I think the best thing to do is arrayadapter. It can specify the data type to be adapted by
over-generics, and then passes the data to be adapted in the constructor.
here because of the data we provide
is a string, so the generic type of arrayadapter is specified as a string, and then in the constructor of the Arrayadapter
Pass in the current context, the ID of the ListView child layout, and the data to be adapted. Note that we used the
Android. R.layout.simple_list_item_1 as the ID of the ListView child layout, which is an Android built-in cloth
Bureau file, there is only one TextView, which can be used to display a text in a simple way. This allows the adapter object to be built.
Finally, you need to call the ListView's Setadapter () method to pass the built-in adapter object in, which
The correlation between the sample ListView and the data is established.
2. Customizing the ListView interface
A ListView that can only display a piece of text is just too dull, and we're now going to do it on the ListView interface
Customizable so that it can display richer content.
First you need to prepare a set of pictures, respectively, corresponding to each of the fruits provided above, we will let these fruit names
There is a pattern next to the name.
Next, define an entity class as the adapter type for the ListView adaptor. To create a new class Fruit, the code looks like this:
Public class Fruit {
private String name;
private int imageId;
Public Fruit (String name, int imageId) {
this.name = name;
This.imageid = imageId;
}
The 2nd chapter starts from the view, explores the activity
131
Public String GetName () {
return name;
}
public int Getimageid () {
return imageId;
}
}
There are only two fields in the Fruit class, name indicates the name of the fruit, and imageId represents the resource ID of the fruit corresponding to the picture.
Then you need to specify a custom layout for the child of the ListView and create a new one in the layout directory
Fruit_item.xml, the code is as follows:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<imageview
android:id= "@+id/fruit_image"
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<textview
android:id= "@+id/fruit_name"
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_gravity= "center"
android:layout_marginleft= "10dip"/>
</LinearLayout>
In this layout, we define a picture of the ImageView used to display the fruit, and define a
TextView is used to display the name of the fruit.
Next you need to create a custom adapter that inherits from Arrayadapter and assigns the generic type
to the Fruit class. To create a new class Fruitadapter, the code looks like this:
Public class Fruitadapter extends arrayadapter<fruit> {
private int resourceId;
First line of code--android
132
Public Fruitadapter (context context, int Textviewresourceid,
list<fruit> objects) {
Super (context, Textviewresourceid, objects);
resourceId = Textviewresourceid;
}
@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
Fruit Fruit = getItem (position);Gets the current item'sFruitInstance
View view = Layoutinflater.from (GetContext ()). Inflate (resourceId, null);
ImageView fruitimage = (ImageView) View.findviewbyid (r.id.fruit_image);
TextView fruitname = (TextView) View.findviewbyid (r.id.fruit_name);
Fruitimage.setimageresource (Fruit.getimageid ());
Fruitname.settext (Fruit.getname ());
return view;
}
}
Fruitadapter overrides a set of constructors for the parent class that will be used to set the ID and sum of the context, ListView child layout, and
The data are passed in. It also overrides the GetView () method, which is used when each child is scrolled into the screen
will be called. In the GetView method, the Fruit instance of the current item is first obtained through the GetItem () method, and then using the
Layoutinflater to load our incoming layout for this subkey, and then call the View's Findviewbyid () method respectively
Gets instances of ImageView and TextView, and calls their Setimageresource () and SetText () parties, respectively
method to set the displayed picture and text, and finally return the layout so that our custom adapter is complete.
The following changes the code in Mainactivity as follows:
Public class Mainactivity extends Activity {
private list<fruit> fruitlist = new arraylist<fruit> ();
@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
initfruits ();//Initializing fruit data
Fruitadapter adapter = new Fruitadapter (Mainactivity.this,
R.layout.fruit_item, fruitlist);
ListView ListView = (ListView) Findviewbyid (R.id.list_view);
The 2nd chapter starts from the view, explores the activity
133
Listview.setadapter (adapter);
}
private void Initfruits () {
Fruit apple = new Fruit ("Apple", r.drawable.apple_pic);
Fruitlist.add (apple);
Fruit banana = new Fruit ("banana", r.drawable.banana_pic);
Fruitlist.add (banana);
Fruit orange = new Fruit ("Orange", r.drawable.orange_pic);
Fruitlist.add (orange);
Fruit watermelon = new Fruit ("Watermelon", r.drawable.watermelon_pic);
Fruitlist.add (watermelon);
Fruit pear = new Fruit ("Pear", r.drawable.pear_pic);
fruitlist.add (pear);
Fruit grape = new Fruit ("Grape", r.drawable.grape_pic);
Fruitlist.add (grape);
Fruit pineapple = new Fruit ("Pineapple", r.drawable.pineapple_pic);
Fruitlist.add (pineapple);
Fruit strawberry = new Fruit ("Strawberry", r.drawable.strawberry_pic);
Fruitlist.add (strawberry);
Fruit cherry = new Fruit ("Cherry", r.drawable.cherry_pic);
Fruitlist.add (cherry);
Fruit mango = new Fruit ("Mango", r.drawable.mango_pic);
Fruitlist.add (mango);
}
}
As you can see, a initfruits () method is added here to initialize all the fruit data. In the Fruit class,
In the constructor, the name of the fruit and the corresponding image ID are passed in, and the created object is added to the fruit list.
We then created the Fruitadapter object in the OnCreate () method and passed the Fruitadapter as an adapter to the
The ListView. This completes the task of customizing the ListView interface.
The most commonly used and hardest to use controls--listview (Android first line code)