从网上参考了一些资料,发现android的SimpleAdapter在ListActivity中使用时,只能加载drawable中的图片,不能加载网络图片
参考了别人写的资料,重写了以下SimpleAdapter的setViewImage(ImageView v, String value)方法
主要参考:http://android.group.iteye.com/group/blog/616262,特此鸣谢!!!
/**********************************************************
* 自己写的Adapter,继承SimpleAdapter,使其可以加载网络图片资源,哇嘎嘎
**********************************************************/
package oneRain.ElcMagazine;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
public class MagazineAdapter extends SimpleAdapter
{
public MagazineAdapter(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to)
{
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
public void setViewImage(ImageView v, String value)
{
URL url = null;
try
{
url = new URL(value);
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpURLConnection conn = null;
try
{
conn = (HttpURLConnection)url.openConnection();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream is = null;
try
{
is = conn.getInputStream();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
try
{
bis.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
is.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
((ImageView) v).setImageBitmap(bm);
}
}