Android basics: displaying network images in listview

Source: Internet
Author: User

Display network images in listview

10:48:22

Recently, I am working on a weather forecast example. I want to add network images to listview. I have read many documents and Google on Baidu for a long time and finally found a solution. Now I want to share with you the solution, this article is just a simple example;

We all know that the imageview class has a setimageuri method, but it cannot directly accept a URI generated by the network address as a parameter to display the image. We have to use its setimagebitmap method, as shown in

 Bitmap mbitmap = NULL; </P> <p> URL url = new URL (imageurl); </P> <p> httpurlconnection conn = (httpurlconnection) URL. openconnection (); </P> <p> inputstream is = Conn. getinputstream (); </P> <p> mbitmap = bitmapfactory. decodestream (is );

A data source is required in simpleadapter to store data. hashmap <> is used to store a bitmap for displaying images. However, when only bitmap is accessed, Images cannot be displayed in listview, we need to process simpleadapter.

The following is the main code for processing simpleadaptr:

Adapter. setviewbinder (New viewbinder () {</P> <p> Public Boolean setviewvalue (view, object data, <br/> string textrepresentation) {<br/> // determine whether it is the object to be processed <br/> If (view instanceof imageview & Data instanceof Bitmap) {<br/> imageview IV = (imageview) view; </P> <p> IV. setimagebitmap (Bitmap) data); <br/> return true; <br/>}else <br/> return false; <br/>}< br/> });

See the API's explanation for Android. widget. simpleadapter. viewbinder:

This class can be used by external clients of simpleadapter to bind
Values to views.
You shoshould use this class to bind values to views that are not
Directly supported by simpleadapter or to change the way binding
Occurs for views supported by simpleadapter.


You can use this class to process data not directly supported by simpleadapter;


Let's take a look at the complete code:

The main. xml file is a listview, And the list_item.xml file provides layout for the controls in the listview file, which is an imageview. These two are relatively simple and do not post code


Main Code mainactivity. Java

Package COM. yin. listview; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import java.net. httpurlconnection; <br/> Import java.net. malformedurlexception; <br/> Import java.net. URL; <br/> Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import Java. util. list; <br/> Import android. app. activity; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapfactory; <br/> Import android. OS. bundle; <br/> Import android. view. layoutinflater; <br/> Import android. view. view; <br/> Import android. widget. imageview; <br/> Import android. widget. listview; <br/> Import android. widget. simpleadapter; <br/> Import android. widget. simpleadapter. viewbinder; <br/> public class mainactivity extends activity {<br/> listview mlistview; <br/> View mview; <br/> Public static final string imageurl = "http://www.google.com//ig/images/weather/mostly_sunny.gif "; <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> mview = layoutinflater. from (this ). inflate (R. layout. list_item, null); <br/> mlistview = (listview) findviewbyid (R. id. MList); <br/> List <pashmap <string, Object> mlistdata = getlistdata (); </P> <p> simpleadapter adapter = new simpleadapter (this, mlistdata, r. layout. list_item, <br/> New String [] {"icon"}, new int [] {R. id. image}); </P> <p> adapter. setviewbinder (New viewbinder () {</P> <p> Public Boolean setviewvalue (view, object data, <br/> string textrepresentation) {<br/> // determine whether it is the object to be processed <br/> If (view instanceof imageview & Data instanceof Bitmap) {<br/> imageview IV = (imageview) view; </P> <p> IV. setimagebitmap (Bitmap) data); <br/> return true; <br/>}else <br/> return false; <br/>}< br/> }); </P> <p> mlistview. setadapter (adapter); <br/>}< br/> public list <pashmap <string, Object> getlistdata () {<br/> List <pashmap <string, object >>> list = new arraylist <pashmap <string, object >>(); <br/> hashmap <string, Object> map = NULL; <br/> for (INT I = 0; I <5; I ++) {<br/> map = new hashmap <string, Object> (); <br/> map. put ("icon", getbitmap (); <br/> list. add (MAP); <br/>}< br/> return list; <br/>}</P> <p> Public bitmap getbitmap () {<br/> bitmap mbitmap = NULL; <br/> try {<br/> URL url = new URL (imageurl); <br/> httpurlconnection conn = (httpurlconnection) URL. openconnection (); <br/> inputstream is = Conn. getinputstream (); <br/> mbitmap = bitmapfactory. decodestream (is); </P> <p >}catch (malformedurlexception e) {<br/> E. printstacktrace (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> return mbitmap; <br/>}< br/>}


Note: In this example, remember to add permissions to display network images.

<Uses-Permission Android: Name = "android. Permission. Internet">




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.