Learn how to display network images using SimpleAdapter for Android

Source: Internet
Author: User

:




The main knowledge point of this program is: SimpleAdapter itself does not support network images, if in Map. in put (a, B), B is a Bitmap. The program outputs a green font on the console instead of a red font, as shown below:

05-10 15:46:45. 474: I/System. out (846): resolveUri failed on bad bitmap uri: android. graphics. Bitmap @ 43e40c08
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + 0qrP68q1z9bP1Mq + expires + cda-vcd4kpha + PHByZSBjbGFzcz0 = "brush: java;"> 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. Let's talk about the first two parameters. view is your custom Layout (such as R. layout. t) for all the components, the program will traverse the components in one layout. data is uploaded by you.

List > (As shown in the getData () method, a list is returned. >), The program traverses each item of data. Then, use the if () statement to filter.


Since it is a network image, you need permissions.

 
 

The Code is as follows:

MainActivity. java

Public class MainActivity extends ActionBarActivity {ListView listView; List
 
  
> Data; String str [] = {"", // Add your own image URL h t p ://"","",""}; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. 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 int [] {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
  
   
> GetData () {data = new ArrayList
   
    
> (); For (int I = 0; I <str. length; I ++) {Map
    
     
Map = new HashMap
     
      
(); Map. put ("images", returnBitMap (str [I]); data. 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 (); InputStream is = conn. getInputStream (); bitmap = BitmapFactory. decodeStream (is); is. close ();} catch (IOException e) {e. printStackTrace () ;}return bitmap ;}}
     
    
   
  
 

Activity_main.xml

 
 
   
 

Custom Layout File

T. xml

 
         
  
 












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.