Android中用ListView显示网络下载的Bitmap

//从网络中下载的数据显示在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;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值