A probe into the listview

Source: Internet
Author: User

The ListView may be one of the most commonly used controls in Android development, but it also requires constant exercise.

Build a simple ListView

1. Add <ListView> tags to the layout file (. xml)

2. Adapter Bindings in Mainactivity.java

  

 1  private  string[] data = {"Apple", " Banana "," Orange "," Watermelon " 2 " Pear "," Grape "," Pineapple "," Strawberry "," Cherry "," Mango "};  3  5  arrayadapter<string> adapter = new  arrayadapter<string>< Span style= "color: #000000;" > ( 6  mainactivity. this  , Android. R.layout.simple_list_item_1, data);  7  listview ListView = (ListView) Findviewbyid ( R.id.list_view);  8  Listview.setadapter (adapter); 

You can see that the contents of the array cannot be added directly to the ListView, so use the adapter.

I used string in the adapter to display the string, and in the future it might add more complex content, such as composite type data.

Arrayadapter has multiple constructors, the values I have here are: The calling class, the ListView style, and the data source.

  In short, the data source is diverse, and to show him to the ListView, we're going to use the adapter.

Create a ListView with pictures

1. Create a class to hold the composite type

2. Create 1 corresponding layout files

3. Set up a dedicated adapter for 1 (extends Arrayadapter)

4. Bind adapter 1 package Com.example.ran.listviewdemo for the ListView in Mainactivity ;

2 3 /**4 * Created by Ran on 2016/3/21.5  */6  Public classMyData7 {
8 PrivateString name;9 Private intimageId; Ten One PublicMyData (String name,intimageId) A { - This. Name =name; - This. ImageId =imageId; the } - PublicString GetName () { - returnname; - } + Public intGetimageid () { - returnimageId; + } A}

imageID is the resource ID of the corresponding picture

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent">5 6     <ImageView7         Android:id= "@+id/iimage"8 Android:layout_width= "Wrap_content"9 Android:layout_height= "Wrap_content" />Ten  One     <TextView A         Android:id= "@+id/iname" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" the         /> - </LinearLayout>
1  PackageCom.example.ran.listviewdemo;2 3 ImportAndroid.content.Context;4 ImportAndroid.view.LayoutInflater;5 ImportAndroid.view.View;6 ImportAndroid.view.ViewGroup;7 ImportAndroid.widget.ArrayAdapter;8 ImportAndroid.widget.ImageView;9 ImportAndroid.widget.TextView;Ten  One Importjava.util.List; A  - /** - * Created by Ran on 2016/3/21. the  */ -  Public classMyDataAdapterextendsArrayadapter<mydata> { -     Private  intresourceId; -  +      PublicMyDataAdapter (Context context,intTextviewresourceid,list<mydata>objects) -     { +         Super(Context, Textviewresourceid, objects); AResourceId =Textviewresourceid; at     } -  - @Override -      PublicView GetView (intposition, View Convertview, viewgroup parent) -     { -MyData MyData =GetItem (position); inView view = Layoutinflater.from (GetContext ()). Inflate (ResourceId,NULL); -ImageView iimage =(ImageView) View.findviewbyid (r.id.iimage); toTextView Iname =(TextView) View.findviewbyid (r.id.iname); + Iimage.setimageresource (Mydata.getimageid ()); - Iname.settext (Mydata.getname ()); the         returnview; *     } $}
1  PackageCom.example.ran.listviewdemo;2 3 Importandroid.support.v7.app.AppCompatActivity;4 ImportAndroid.os.Bundle;5 ImportAndroid.widget.ListView;6 7 Importjava.util.ArrayList;8 Importjava.util.List;9 Ten  Public classMainactivityextendsappcompatactivity { One  A     PrivateList<mydata> dataList =NewArraylist<mydata>(); -  - @Override the     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - InitData (); +MyDataAdapter adapter =NewMyDataAdapter (mainactivity. -                  This, r.layout.data_item,datalist); +ListView ListView =(ListView) Findviewbyid (R.id.list_view); A Listview.setadapter (adapter); at     } -  -     Private voidInitData () -     { -MyData T1 =NewMyData ("You are so naughty, come, don't bb", r.mipmap.t1); -MyData t2 =NewMyData ("You are so naughty, come, don't bb", r.mipmap.t2); inMyData t3 =NewMyData ("You are so naughty, come, don't bb", R.MIPMAP.T3); -MyData T4 =NewMyData ("You are so naughty, come, don't bb", R.MIPMAP.T4); toMyData T5 =NewMyData ("You are so naughty, come, don't bb", R.MIPMAP.T5); +MyData T6 =NewMyData ("You are so naughty, come, don't bb", R.MIPMAP.T6); -MyData T7 =NewMyData ("You are so naughty, come, don't bb", r.mipmap.t7); theMyData T8 =NewMyData ("You are so naughty, come, don't bb", r.mipmap.t8); *  $ datalist.add (t1);Panax Notoginseng Datalist.add (T2); - Datalist.add (T3); the datalist.add (T4); + Datalist.add (T5); A Datalist.add (T6); the Datalist.add (T7); + Datalist.add (T8); -  $  $     } -}

A probe into the listview

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.