Android realizes listview asynchronous Load network picture and dynamically updates the method _android

Source: Internet
Author: User
Tags sqlite database

This article describes the Android implementation ListView asynchronous loading of network pictures and dynamic update methods. Share to everyone for your reference, specific as follows:

Application Example: Parsing the data returned in the background, each display in the ListView, including the activity picture, store name, activity details, address, telephone and distance.

Definition of ListView in Layout file:

<listview
android:id= "@id/maplistview" 
android:background= "@drawable/bg" android:layout_width= " 
Fill_parent " 
android:layout_height=" fill_parent " 
android:cachecolorhint=" #00000000 " 
android: divider= "@drawable/separator" 
android:dividerheight= "2.0px" 
android:layout_below= "@id/mapseparator"
/>

Define the layout of the active picture, store name, activity details, address, phone, and distance in the layout file ListViewItem

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout android:id= "@+id/relativelayout01" Android:layout_ Width= "Fill_parent" xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_height= "WRAP_"
Content "android:paddingbottom=" 2dip "android:paddingleft=" 2dip "android:paddingright=" 2dip "> <imageview android:paddingtop= "2dip" android:layout_alignparentleft= "true" android:layout_width= "80px" android:layout_height = "80px" android:id= "@+id/maplistviewitemimage"/> <textview android:layout_height= "Wrap_content" Android: Textsize= "17dip" android:layout_width= "fill_parent" android:id= "@+id/maplistviewitemshopname" Android:layout_ torightof= "@id/maplistviewitemimage" android:textcolor= "#000000"/> <textview android:layout_height= "Wrap_ Content "Android:layout_width=" Fill_parent "android:layout_alignparentleft=" true "android:layout_below=" @+id/ Maplistviewitemimage "android:id=" @+id/maplistviewitemacti "android:textcolor=" #6C6C6C "/> <TExtview android:layout_height= "wrap_content" android:layout_width= "Fill_parent" True "android:layout_below=" @+id/maplistviewitemacti "android:id=" @+id/maplistviewitemaddr "android:textColor=" # 6C6C6C "android:singleline=" true "/> <textview android:layout_height=" wrap_content "android:layout_width=" Fill_parent "android:layout_alignparentleft=" true "android:layout_below=" @+id/maplistviewitemaddr "android:id=" @+

 Id/maplistviewitemtelphone "android:textcolor=" #6C6C6C "android:singleline=" true "/> </RelativeLayout>

(1) Defines the contents of the control in the ListViewItem class Maplistimageandtext management

Package com.google.zxing.client.android.AsyncLoadImage;
    public class Maplistimageandtext {private String imageUrl;
    Private String Shopname;
    Private String Activitynifo;
    Private String address;
    Private String telephone;
    private String distance; Public Maplistimageandtext (String imageUrl, String shopname, String Activitynifo, string address, string telephone,
      String distance) {this.imageurl = IMAGEURL;
      This.shopname = Shopname;
      This.activitynifo = Activitynifo;
      this.address = address;
      This.telephone = telephone;
    This.distance=distance;
    Public String Getimageurl () {return imageUrl;
    Public String Getshopname () {return shopname;
    Public String Getactivitynifo () {return activitynifo;
    Public String getaddress () {return address;
    Public String Gettelephone () {return telephone;
    Public String getdistance () {return distance;
}
}
 

(2) to define a class Maplistviewcache to instantiate a control in ListViewItem

Package com.google.zxing.client.android.AsyncLoadImage;
Import COM.GOOGLE.ZXING.CLIENT.ANDROID.R;
Import Android.view.View;
Import Android.widget.ImageView;
Import Android.widget.TextView;
    public class Maplistviewcache {private View baseview;
    Private TextView Shopname;
    Private TextView Activitynifo;
    Private TextView address;
    Private TextView telephone;
    private TextView distance;
    Private ImageView ImageView;
    Public Maplistviewcache (View baseview) {this.baseview = Baseview; Public TextView Getshopname () {if (Shopname = null) {Shopname = (TextView) Baseview.findviewbyid (R.
      Id.maplistviewitemshopname);
    return shopname; Public TextView Getactivitynifo () {if (Activitynifo = null) {Activitynifo = (TextView) baseview.fin
      Dviewbyid (R.ID.MAPLISTVIEWITEMACTI);
    return ACTIVITYNIFO; Public TextView getaddress () {if (address = = NULL) {address = (TextView) Baseview.findviewbyid (R.ID.MAPLISTVIEWITEMADDR);
    return to address; Public TextView Gettelephone () {if (telephone = null) {telephone = (TextView) Baseview.findviewbyid
      (R.id.maplistviewitemtelphone);
    return telephone; Public ImageView Getimageview () {if (ImageView = null) {ImageView = (ImageView) Baseview.findviewby
      Id (R.id.maplistviewitemimage);
    return ImageView; Public TextView getdistance () {if (distance = null) {distance = (TextView) Baseview.findviewbyid (R.
      Id.maplistviewitemdistance);
    return distance;

 }
}

(3) Define class Asyncimageloader, open thread download specified picture

Package com.google.zxing.client.android.AsyncLoadImage;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.lang.ref.SoftReference;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import Java.util.HashMap;
Import android.graphics.drawable.Drawable;
Import Android.os.Handler;
Import Android.os.Message;
     public class Asyncimageloader {private hashmap<string, softreference<drawable>> Imagecache;
     Public Asyncimageloader () {Imagecache = new hashmap<string, softreference<drawable>> (); Public drawable loaddrawable (final String imageUrl, final imagecallback imagecallback) {if (Imagecache.cont
         Ainskey (IMAGEURL)) {softreference<drawable> softreference = Imagecache.get (IMAGEURL);
         drawable drawable = Softreference.get ();
         if (drawable!= null) {return drawable; } final Handler Handler = new Handler () {public void Handlemessage (MessaGE message) {imagecallback.imageloaded ((drawable) message.obj, IMAGEURL);
       }
       };
           New Thread () {@Override public void run () {drawable drawable = Loadimagefromurl (IMAGEURL);
           Imagecache.put (IMAGEURL, New softreference<drawable> (drawable));
           Message message = Handler.obtainmessage (0, drawable);
         Handler.sendmessage (message);
       }}.start ();
     return null;
      public static drawable loadimagefromurl (String url) {URL m;
      InputStream i = null;
        try {m = new URL (URL);
      i = (InputStream) m.getcontent ();
      catch (Malformedurlexception E1) {e1.printstacktrace ();
      catch (IOException e) {e.printstacktrace ();
      } drawable D = Drawable.createfromstream (i, "src");
    return D;
     Public interface Imagecallback {public void imageloaded (drawable imagedrawable, String imageUrl);

}
} 

The

(4) Defines the class Maplistimageandtextlistadapter inheritance Arrayadapter, which is used to create the Asyncimageloader instance and specify the contents of the control

Package com.google.zxing.client.android.AsyncLoadImage;
Import java.util.List;
Import COM.GOOGLE.ZXING.CLIENT.ANDROID.R;
Import Com.google.zxing.client.android.AsyncLoadImage.AsyncImageLoader.ImageCallback;
Import android.app.Activity;
Import android.graphics.drawable.Drawable;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.ArrayAdapter;
Import Android.widget.ImageView;
Import Android.widget.ListView;
Import Android.widget.TextView; public class Maplistimageandtextlistadapter extends arrayadapter<maplistimageandtext> {private ListView Listvie
    W
    Private Asyncimageloader Asyncimageloader; Public Maplistimageandtextlistadapter, list<maplistimageandtext> imageandtexts, ListView
      ListView) {Super (activity, 0, imageandtexts);
      This.listview = ListView;
    Asyncimageloader = new Asyncimageloader (); public view GetView (int position, view Convertview, ViewGroup Parent) {Activity activity = (activity) getcontext ();
      Inflate the views from XML View Rowview = Convertview;
      Maplistviewcache Viewcache;
        if (Rowview = = null) {Layoutinflater inflater = Activity.getlayoutinflater ();
        Rowview = inflater.inflate (R.layout.maplistviewitem, NULL);
        Viewcache = new Maplistviewcache (Rowview);
      Rowview.settag (Viewcache);
      else {Viewcache = (Maplistviewcache) rowview.gettag ();
      } maplistimageandtext ImageAndText = GetItem (position);
      Load the image and set it on the ImageView String imageUrl = Imageandtext.getimageurl ();
      ImageView ImageView = Viewcache.getimageview ();
      Imageview.settag (IMAGEURL); drawable cachedimage = asyncimageloader.loaddrawable (ImageUrl, New Imagecallback () {public void imageloaded (Drawa 
    Ble imagedrawable, String imageUrl) {ImageView Imageviewbytag = (imageview) listview.findviewwithtag (IMAGEURL);      if (Imageviewbytag!= null) {imageviewbytag.setimagedrawable (imagedrawable);
      }
        }
      });
      if (cachedimage = = null) {Imageview.setimageresource (R.drawable.refresh);
      }else{imageview.setimagedrawable (cachedimage);
      }//Set the text on the TextView TextView shopname = Viewcache.getshopname ();
      Shopname.settext (Imageandtext.getshopname ());
      TextView Activitynifo = Viewcache.getactivitynifo ();
      Activitynifo.settext (Imageandtext.getactivitynifo ());
      TextView address = viewcache.getaddress ();
      Address.settext (Imageandtext.getaddress ());
      TextView telephone = Viewcache.gettelephone ();
      Telephone.settext (Imageandtext.gettelephone ());
      TextView distance = Viewcache.getdistance ();
      Distance.settext (Imageandtext.getdistance ());
    return rowview;

 }
}

(5) Binding of ListView and Maplistimageandtextlistadapter in the main program

Tuangoupoints the string
string[] mtuangoupoints =tuangoupoints.split ("@") after parsing the data returned by the background;
List<maplistimageandtext> dataarray=new arraylist<maplistimageandtext> ();
for (int i=0; i<mtuangoupoints.length;i++) {
  string[] tonepoint=mtuangoupoints[i].split ("#");
  String shopname=string.valueof (i+1) +tonepoint[2];
  String activityinfo=tonepoint[1];
  String address=tonepoint[6];
  String telephone=tonepoint[7];
  String imageurl=tonepoint[8];
  String distance=tonepoint[5];
  Maplistimageandtext test=new Maplistimageandtext (imageurl,shopname,activityinfo,address,telephone,distance);
  Dataarray.add (test);
}
Maplistimageandtextlistadapter adapter=new Maplistimageandtextlistadapter (This, DataArray, MlistView);
Mlistview.setadapter (adapter);

For more information on Android-related content readers can view the site: "Android View tips Summary", "Android Programming activity Operating Skills Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", " Android Resource Operation tips Summary and the "Android Controls usage Summary"

I hope this article will help you with the Android program.

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.