Android中用ListView顯示網路下載的Bitmap

來源:互聯網
上載者:User
//從網路中下載的資料顯示在listview中的處理方法,重點是顯示bitmap問題的處理,直接粘上代碼,以供將來參考。public class ViewDianNao extends Activity {private LinearLayout myListLayout; // 聲明LinearLayout類型變數private ListView tripListView;// 聲明ListView類型變數private ProgressDialog myDialog;// 聲明ProgressDialog類型變數private List<Goods> goodsList;// 聲明List類型變數@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.viewdiannao);// 載入viewtuijian.xml資源檔myListLayout = (LinearLayout) this.findViewById(R.id.tripList);// 擷取資源檔中的LinearLayouttripListView = 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清單項目單擊事件@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1,int position, long id) {// TODO Auto-generated method stubGoods 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添加適配器adapteradapter.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;}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.