ListView 中显示图片

大家知道ImageView 类虽然有一个 setImageUri 方法,但不能直接接受一个由网络地址生成的uri作为参数从而显示图片,我们只好使用其 setImageBitmap 方法,如

Bitmap mBitmap = null;  
     
URL url = new URL(imageUrl);  
              
HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
              
InputStream is = conn.getInputStream();  
              
mBitmap = BitmapFactory.decodeStream(is);  
  在SimpleAdapter中需要一个数据源,用来存储数据的,在显示图片时我们要用HashMap<>存储一个Bitmap;但仅存取了Bitmap时在ListView中是无法显示图片的,我们需要对SimpleAdapter进行处理 。

如下是对SimpleAdaptr处理的主要代码:adapter.setViewBinder(new ViewBinder() {

              
            public 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;  
                }else  
                return false;  
            }  
        });  
  看API对Android.widget.SimpleAdapter.ViewBinder的解释:

This class can be used by external clients of SimpleAdapter to bind values to views. You should use this class to bind values to views that are not directly supported by SimpleAdapter or to change the way binding occurs for views supported by SimpleAdapter.


你可以使用该类来处理SimpleAdapter不直接支持的数据;


下面来看看完整的代码:

main.xml 中就是一个ListView  , list_item.xml 是为ListView中控件提供布局 ,就是一个ImageView。这两个比较简单,不贴出代码


主要代码   MainActivity.Java

package com.yin.listView;  
import java.io.IOException;  
import java.io.InputStream;  
import java.NET.HttpURLConnection;  
import java.net.MalformedURLException;  
import java.net.URL;  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import android.app.Activity;  
import android.graphics.Bitmap;  
import android.graphics.BitmapFactory;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.widget.ImageView;  
import android.widget.ListView;  
import android.widget.SimpleAdapter;  
import android.widget.SimpleAdapter.ViewBinder;  
public class MainActivity extends Activity {  
    ListView mListView;  
    View mView;  
    public static final String imageUrl = "http://www.google.com//ig/images/weather/mostly_sunny.gif";  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        mView = LayoutInflater.from(this).inflate(R.layout.list_item,null);  
        mListView = (ListView) findViewById(R.id.mList);  
        List<HashMap<String,Object>> mListData = getListData();  
          
        SimpleAdapter adapter = new SimpleAdapter(this, mListData, R.layout.list_item,  
                                new String[]{"icon"}, new int[]{R.id.image});  
         
        adapter.setViewBinder(new ViewBinder() {  
              
            public 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;  
                }else  
                return false;  
            }  
        });  
          
        mListView.setAdapter(adapter);  
    }  
    public List<HashMap<String,Object>> getListData(){  
        List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();  
        HashMap<String,Object> map = null;  
        for(int i=0;i<5;i++){  
            map = new HashMap<String, Object>();  
            map.put("icon",getBitmap());  
            list.add(map);  
        }  
        return list;  
    }  
      
    public Bitmap getBitmap(){  
        Bitmap mBitmap = null;  
        try {  
            URL url = new URL(imageUrl);  
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
            InputStream is = conn.getInputStream();  
            mBitmap = BitmapFactory.decodeStream(is);  
              
        } catch (MalformedURLException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        
        return mBitmap;  
    }  
}  
 
<uses-permission android:name="android.permission.INTERNET">  
 

你可能感兴趣的:(java,android,.net,Google,OS)