Android Development Dry: Implementing a ListView loading picture asynchronously
Source: Internet
Author: User
<span id="Label3"></p><p>For the ListView asynchronous load Picture This problem, wheat College Android development teacher said a very practical method, wheat College Android development teacher says that anything is to get pictures through the network general Use this method is better, the user experience is good, the following said the implementation method, Paste the code of the Main method first:<br>Package cn.wangmeng.test;<br><br>Import java.io.IOException;<br>Import java.io.InputStream;<br>Import java.lang.ref.SoftReference;<br>Import java.net.MalformedURLException;<br>Import java.net.URL;<br>Import java.util.HashMap;<br><br>Import android.graphics.drawable.Drawable;<br>Import android.os.Handler;<br>Import android.os.Message;<br><br>public class Asyncimageloader {<br><br>Private hashmap<string, softreference<drawable>> imagecache;<br><br>Public Asyncimageloader () {<br>Imagecache = new hashmap<string, softreference<drawable>> ();<br>}<br><br>Public drawable loaddrawable (final String imageUrl, final imagecallback Imagecallback) {<br>If (imagecache.containskey (imageUrl)) {<br>softreference<drawable> softreference = Imagecache.get (imageUrl);<br>drawable drawable = Softreference.get ();<br>If (drawable! = Null) {<br>Return drawable;<br>}<br>}<br>Final Handler Handler = new Handler () {<br>public void Handlemessage (message Message) {<br>imagecallback.imageloaded ((drawable) message.obj, imageUrl);<br>}<br>};<br>New Thread () {<br>@Override<br>public void Run () {<br>drawable drawable = Loadimagefromurl (imageUrl);<br>Imagecache.put (imageUrl, New Softreference<drawable> (drawable));<br>Message message = Handler.obtainmessage (0, drawable);<br>Handler.sendmessage (message);<br>}<br>}.start ();<br>Return null;<br>}<br><br>public static drawable Loadimagefromurl (String Url) {<br>URL m;<br>InputStream i = null;<br>try {<br>m = new URL (url);<br>i = (inputstream) m.getcontent ();<br>} catch (malformedurlexception E1) {<br>E1.printstacktrace ();<br>} catch (ioexception E) {<br>E.printstacktrace ();<br>}<br>drawable d = Drawable.createfromstream (i, "src");<br>Return d;<br>}<br><br>Public interface Imagecallback {<br>public void imageloaded (drawable imagedrawable, String imageUrl);<br>}<br><br>}<br><br>The above code is the main method to achieve the asynchronous acquisition of pictures, SoftReference is a soft reference, is to better for the system to reclaim variables, duplicate URLs directly return the existing resources, implement the callback function, let the data succeed, update to the UI Thread.<br>Several auxiliary class Files:<br>Package cn.wangmeng.test;<br><br>public class ImageAndText {<br>Private String imageUrl;<br>Private String text;<br><br>Public ImageAndText (string imageUrl, string Text) {<br>This.imageurl = imageUrl;<br>This.text = text;<br>}<br>Public String Getimageurl () {<br>Return imageUrl;<br>}<br>Public String GetText () {<br>Return text;<br>}<br>}<br>Package cn.wangmeng.test;<br><br>Import android.view.View;<br>Import android.widget.ImageView;<br>Import android.widget.TextView;<br><br>public class Viewcache {<br><br>Private View baseview;<br>Private TextView TextView;<br>Private ImageView ImageView;<br><br>Public Viewcache (View Baseview) {<br>This.baseview = baseview;<br>}<br><br>Public TextView Gettextview () {<br>if (textView = = Null) {<br>TextView = (textView) Baseview.findviewbyid (r.id.text);<br>}<br>Return textView;<br>}<br><br>Public ImageView Getimageview () {<br>if (imageView = = Null) {<br>ImageView = (imageView) Baseview.findviewbyid (r.id.image);<br>}<br>Return imageView;<br>}<br><br>}<br><br>Viewcache is a sub-element layout that assists in getting adapter<br><br>Package cn.wangmeng.test;<br><br>Import java.util.List;<br><br>Import cn.wangmeng.test.AsyncImageLoader.ImageCallback;<br><br>Import android.app.Activity;<br>Import android.graphics.drawable.Drawable;<br>Import android.view.LayoutInflater;<br>Import android.view.View;<br>Import android.view.ViewGroup;<br>Import android.widget.ArrayAdapter;<br>Import android.widget.ImageView;<br>Import android.widget.ListView;<br>Import android.widget.TextView;<br><br>public class Imageandtextlistadapter extends Arrayadapter<imageandtext> {<br><br>Private ListView listview;<br>Private Asyncimageloader asyncimageloader;<br><br>Public imageandtextlistadapter (activity activity, list<imageandtext> imageandtexts, listview Listview) {<br>Super (activity, 0, imageandtexts);<br>This.listview = listView;<br>Asyncimageloader = new Asyncimageloader ();<br>}<br><br>Public View getView (int position, View convertview, viewgroup Parent) {<br>Activity activity = (activity) GetContext ();<br><br>Inflate the views from XML<br>View Rowview = convertview;<br>Viewcache viewcache;<br>if (rowview = = Null) {<br>Layoutinflater Inflater = Activity.getlayoutinflater ();<br>Rowview = Inflater.inflate (r.layout.image_and_text_row, null);<br>Viewcache = new Viewcache (rowview);<br>Rowview.settag (viewcache);<br>} else {<br>Viewcache = (viewcache) Rowview.gettag ();<br>}<br>ImageAndText ImageAndText = GetItem (position);<br><br>Load the image and set it on the ImageView<br>String IMAGEURL = Imageandtext.getimageurl ();<br>ImageView ImageView = Viewcache.getimageview ();<br>Imageview.settag (imageUrl);<br>drawable cachedimage = asyncimageloader.loaddrawable (imageUrl, new Imagecallback () {<br>public void imageloaded (drawable imagedrawable, String ImageUrl) {<br>ImageView Imageviewbytag = (ImageView) Listview.findviewwithtag (imageUrl);<br>If (imageviewbytag! = Null) {<br>Imageviewbytag.setimagedrawable (imagedrawable);<br>}<br>}<br>});<br>if (cachedimage = = Null) {<br>Imageview.setimageresource (r.drawable.default_image);<br>}else{<br>Imageview.setimagedrawable (cachedimage);<br>}<br>Set the text on the TextView<br>TextView TextView = Viewcache.gettextview ();<br>Textview.settext (imageandtext.gettext ());<br><br>Return rowview;<br>}<br><br>}<br><br>Imageandtextlistadapter is the implementation of the ListView adapter, there is a trick is Imageview.settag (imageUrl), Settag is to store data, so as to ensure that the callback function, ListView to update their corresponding item, we read carefully to KNOW.<br>Finally post the layout file:<br><br><?xml version= "1.0" encoding= "utf-8"?><br><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"<br>android:orientation= "horizontal"<br>Android:layout_width= "fill_parent"<br>android:layout_height= "wrap_content" ><br><br><imageview android:id= "@+id/image"<br>Android:layout_width= "wrap_content"<br>android:layout_height= "wrap_content"<br>/><br><br><textview android:id= "@+id/text"<br>Android:layout_width= "wrap_content"<br>android:layout_height= "wrap_content"/><br><br></LinearLayout><br><br></p><p><p>Friends who want to develop communication with Android can join: 362057824.</p></p><br>More Android Development Tutorials At: Http://www.maiziedu.com/course/android/<br><br><p><p>Android Development Dry: Implementing a ListView loading picture asynchronously</p></p></span>
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