:
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