Android--listview

Source: Internet
Author: User
Tags blizzard

Introduction to Google's official documents: https://developer.android.com/reference/android/widget/ListView.html

Displays a collection of views that can be scrolled vertically, where each view is immediately below the previous view in the list. For a more modern, more flexible and more efficient display of the list, use Recyclerview ...

Now add the ListView to the active layout:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" Match_parent "    android:layout_height=" Match_parent "     android:orientation= "vertical" >   <ListView       android:id= "@+id/list_view"        android:layout_width= "Match_parent"       android:layout_height= "Match_parent"/></ Linearlayout>

Here is a custom list, which is the layout of your own custom list. Create a new Hero_item.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal" > <ImageView Android:id= "@+id/hero_img"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <TextView Android:id= "@+id/hero_name"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center_vertical"android:textsize= "20SP"/></linearlayout>

And then the data that you want to use, http://ow.blizzard.cn/heroes/, data from the Blizzard's Overwatch website Hero Introduction page

Next to the HTML code is the URL of the hero avatar,</span><span class= " Portrait-title "> The Doomsday Fist </span></span> is the hero's name.

So create a new H

 Public classHero {PrivateString ImageUrl; PrivateString name;  PublicHero (String imageUrl, string name) { This. IMAGEURL =ImageUrl;  This. Name =name; }     PublicString GetUrl () {returnImageUrl; }     Public voidseturl (String url) { This. IMAGEURL =URL; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}

Then is the ListView adapter, create a new Heroadapter.class, this adapter inherit from Arrayadapter, and the generic is specified as hero

 Public classHeroadapterextendsArrayadapter {    Private intresourceId;  PublicHeroadapter (Context context,intTextviewresourceid, listobjects) {        Super(context,textviewresourceid,objects); ResourceId=Textviewresourceid; } @NonNull @Override PublicView GetView (intposition, @Nullable View Convertview, @NonNull viewgroup parent) {Hero Hero=GetItem (position); View View= Layoutinflater.from (GetContext ()). Inflate (Resourceid,parent,false); ImageView heroimg=(ImageView) View.findviewbyid (r.id.hero_img); TextView Heroname=(TextView) View.findviewbyid (r.id.hero_name);        Glide.with (GetContext ()). Load (Hero.geturl ()). into (HEROIMG);        Heroname.settext (Hero.getname ()); returnview; }}

Heroadapter overrides the constructor of the parent class, obtains the context, the ID and data of the ListView's child layout, and then overrides the GetView () method, which is called when each subkey is scrolled to the screen, in GetItem () You get the current hero instance based on position, then use Layoutinflater to load the layout for this subkey, then set the ImageView and TextView in the layout, and finally return to the layout.

The code in the last activity

 Public classMainactivityextendsappcompatactivity {PrivateListNewArraylist<>(); PrivateHandler Handler; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); FinalListView ListView =(ListView) Findviewbyid (R.id.list_view);        Initheroes (); Handler=NewHandler () {@Override Public voidhandlemessage (Message msg) {if(Msg.what = = 1) {Heroadapter adapter=NewHeroadapter (mainactivity. This, R.layout.hero_item, herolist);                Listview.setadapter (adapter);    }            }        }; }    //reading Data    Private voidinitheroes () {String Weatherurl= "Http://ow.blizzard.cn/heroes/"; Httputil.sendokhttprequest (Weatherurl,NewCallback () {@Override Public voidOnresponse (call call, Response Response)throwsIOException {FinalString ResponseText =response.body (). String (); NewThread (NewRunnable () {@Override Public voidrun () {Document doc= Jsoup.parse (responsetext);//converting a string type of HTML to documentElements elements1 = Doc.select (". Portrait");//Read image URLElements elements2 = Doc.select (". Portrait-title");//Read Hero name                         for(intj = 0; J < Elements1.size (); J + +) {Hero Hero=NewHero (Elements1.get (j). attr ("src"), Elements2.get (j). text ());                        Herolist.add (Hero); } Message msg=NewMessage (); Msg.what= 1;                    Handler.sendmessage (msg);            }}). Start (); } @Override Public voidonfailure (call call, IOException e) {e.printstacktrace (); Runonuithread (NewRunnable () {@Override Public voidrun () {Toast.maketext (mainactivity. This, "Data acquisition failed", Toast.length_short). Show ();            }                });    }        }); }}

Create a heroadapter, pass in the context, the sub-layout and the data, and transfer the adapter to the ListView.

Android--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.