Learn Android simpleadapter Display Network pictures

Source: Internet
Author: User




The main knowledge of this program is: Simpleadapter itself is not support network pictures, if in Map.put (A, A, b) is a bitmap, the program does not report red font, but in the console output green font, as follows

05-10 15:46:45.474:i/system.out (846): ResolveUri failed on bad bitmap uri: [email protected]

To implement the display network picture is actually very simple, using the method in Simpleadapter Simpleadapter.setviewbinder ().


Simpleadapter.setviewbinder (New Viewbinder () {@Overridepublic Boolean setviewvalue (View view, Object data,string Textrepresentation) {if (view instanceof ImageView && data instanceof Bitmap) {ImageView IV = (ImageView) view;iv. Setimagebitmap ((Bitmap) data); return true;} return false;}});


New Simpleadapter (Mainactivity.this,getdata (), r.layout.t, new string[] {"Images"},new int[] {r.id.imageview_main});


There are three parameters view,data,textrepresentation, say the first two parameters, view is your custom layout (such as R.LAYOUT.T) all the components, the program will traverse the component in the layout, data is you preach

list<map<, >> (GetData () method, returns a list<map<, >>), the program iterates through each item of data. Next, use the IF () statement to filter on it.


Since it is a network picture, it requires permission

<uses-permission android:name= "Android.permission.INTERNET"/>

The code is as follows:

Mainactivity.java

public class Mainactivity extends Actionbaractivity {listview listview; list<map<string,?>> data; String str[] = {"",//Add image URL for h t T P:/"", "", ""}; @Overridepublic void OnCreate (Bundle savedinstancestate) {s Uper.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); listview = (ListView) Findviewbyid ( R.id.lv_main); Simpleadapter simpleadapter = new Simpleadapter (Mainactivity.this,getdata (), r.layout.t, new string[] {"Images"},new in T[] {r.id.imageview_main}), Simpleadapter.setviewbinder (new Viewbinder () {@Overridepublic Boolean setviewvalue (View View, Object data,string textrepresentation) {if (view instanceof ImageView && data instanceof Bitmap) {ImageView IV = (ImageView) view;iv.setimagebitmap ((Bitmap) data); return true;} return false;}}); Listview.setadapter (Simpleadapter);} Public list<map<string,?>> GetData () {data = new arraylist<map<string,?>> (); for (int i = 0; I &l T Str.length; i++) {map<string, OBJECT&GT Map = new hashmap<string, object> (), Map.put ("Images", Returnbitmap (Str[i]));d ata.add (map); return data;} Public Bitmap returnbitmap (String url) {URL myfileurl = null; Bitmap Bitmap = null;try {myfileurl = new URL (URL),} catch (Malformedurlexception e) {e.printstacktrace ();} try {HttpURLConnection conn = (httpurlconnection) myfileurl.openconnection (); Conn.setdoinput (true); Conn.connect (); I Nputstream is = Conn.getinputstream (), bitmap = Bitmapfactory.decodestream (is); Is.close (); catch (IOException e) {e.printstacktrace ();} return bitmap;}}

Activity_main.xml

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

customizing layout files

T.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:o rientation= "vertical" >        <imageview         android:id= "@+id/imageview_main"        android:layout_width= " 80DP "        android:layout_height=" 80DP "/></linearlayout>












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.