Android中用ListView显示网络下载的Bitmap
2012-06-23 19:32
274 查看
//从网络中下载的数据显示在listview中的处理方法,重点是显示bitmap问题的处理,直接粘上代码,以供将来参考。 public class ViewDianNao extends Activity { private LinearLayout myListLayout; // 声明LinearLayout类型变量 private ListView tripListView;// 声明ListView类型变量 private ProgressDialog myDialog;// 声明ProgressDialog类型变量 private List<Goods> goodsList;// 声明List类型变量 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.viewdiannao);// 加载viewtuijian.xml资源文件 myListLayout = (LinearLayout) this.findViewById(R.id.tripList);// 获取资源文件中的LinearLayout tripListView = new ListView(this);// 创建ListView对象 // 创建LinearLayout.LayoutParams类型对象 LinearLayout.LayoutParams tripListViewParam = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); tripListView.setCacheColorHint(Color.WHITE); myListLayout.addView(tripListView, tripListViewParam);// 将tripListView添加到myListLayout布局上 getGoodsList();// 读取商品列表 tripListView.setOnItemClickListener(new OnItemClickListener() {// tripListView列表项单击事件 @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) { // TODO Auto-generated method stub Goods theGood = goodsList.get(position);// 获取当前列表项选中的商品 Intent it = new Intent();//创建Intent对象 Bundle bundle = new Bundle();//创建Bundle对象 it.setClass(ViewDianNao.this, ShangPinDetailView.class); bundle.putSerializable("GoodObj", (Serializable) theGood); it.putExtras(bundle); startActivity(it); } }); } // 读取商品列表数据 private void getGoodsList() { myDialog = ProgressDialog.show(ViewDianNao.this, "请稍等...", "数据检索中...", true); new Thread() { public void run() { try { goodsList = new ConnectWeb().getTypeList(3);//获取电脑办公类别商品列表,3表示类别为电脑办公 Message m = new Message(); listHandler.sendMessage(m); } catch (Exception e) { e.printStackTrace(); } finally { myDialog.dismiss(); } } }.start(); } Handler listHandler = new Handler() { public void handleMessage(Message msg) { if (goodsList.size() == 0) { return; } showGoodsList();//填充路书列表适配器 } }; /** * 填充路书列表适配器 */ public void showGoodsList() { SimpleAdapter adapter = new SimpleAdapter(this, getTripList(), R.layout.tuijianrow, new String[] { "img", "name", "money", "zhe" }, new int[] { R.id.tripImg, R.id.tripTitle, R.id.tripSegName, R.id.tripProv }); tripListView.setAdapter(adapter);//为tripListView添加适配器adapter adapter.setViewBinder(new ViewBinder() { public boolean setViewValue(View arg0, Object arg1, String textRepresentation) { if ((arg0 instanceof ImageView) & (arg1 instanceof Bitmap)) { ImageView imageView = (ImageView) arg0; Bitmap bitmap = (Bitmap) arg1; imageView.setImageBitmap(bitmap); return true; } else { return false; } } }); } public List<Map<String, Object>> getTripList() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); for (int i = 0; i < goodsList.size(); i += 1) { Map<String, Object> map = new HashMap<String, Object>(); Goods goods = goodsList.get(i); try { URL picUrl = new URL(goods.getDir() + "/" + goods.getPic()); Bitmap pngBM = BitmapFactory.decodeStream(picUrl.openStream()); map.put("img", pngBM); } catch (Exception e) { e.printStackTrace(); } map.put("name", "商品名称:"+goods.getBrand()); map.put("money", "商品价格:"+"¥" + goods.getPrice()); map.put("zhe", "商品折扣:"+goods.getDiscount()); list.add(map); } return list; } }
相关文章推荐
- Android:ListView异步加载图片(实现网络下载、存储本地、缓存内存、压缩显示)
- 技术篇-android ListView网络图片下载文件缓存
- Android基础篇之在ListView中显示网络图片
- Android-AsyncTask结合Listview 下载显示
- android ImageLoader 缓存下载显示网络图片
- Android 通过开源框架实现加载网络图片并下载到SD卡通知系统相册显示,(并实现分享图片功能)
- Android关于ListView显示SD卡上的Bitmap的图片(图片压缩之后)
- 历史版本1:Android基础篇之在ListView中显示网络图片
- Android ListView从网络获取图片及文字显示
- Android中带有进度条百分比显示的可以从网络下载文件的适配器
- [Android实例] Android ListView从网络获取图片及文字显示 + 自己搭建的服务器的xml文件
- Android ListView从网络获取图片及文字显示
- ListView通过自定义的Adapter实现异步下载显示网络图片
- Android中用BaseAdapter实现带Button和显示网络图片的Listview
- Android异步下载网络图片&android解析xml文件的方式&使用Adapter为ListView提供数据
- Android实习04:ListView网络异步加载图片的优化显示(2)
- Android ListView从网络获取图片及文字显示
- ListView通过自定义的Adapter实现异步下载显示网络图片
- android下载网络图片,BitmapFactory创建bitmap
- Android中从网络上异步下载图片并显示到手机屏幕上