android源代码学习.doc_第1页
android源代码学习.doc_第2页
android源代码学习.doc_第3页
android源代码学习.doc_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

本文由麦可网/ 收集整理,转载请注明出处。android 为 ListView Item中的组件添加事件 以及更新数据/ 部分代码如下:其中holder.count 是一个EditViewholder.price 是一个TextViewJava代码1 viewplaincopytoclipboardprint?2 Override3 publicViewgetView(finalintposition,ViewconvertView,finalViewGroupparent)4 。5 /注意该方法中的Item组件不能使用holder模式,如果用该模式,所有的组件将共享item中view的事件6 导致监听不到指定item中的view的事件,解决办法就是每次创建一个Item中的组件78 然后对于每个item使用不同的监听事件即newTextWatcher()每次都创建一个新的事件监听器910 finalViewHolderholder=newViewHolder();1112 holder.count.addTextChangedListener(newTextWatcher()13 .14 Override15 publicvoidafterTextChanged(Editables)16 /holder.price是与holder.count在同一个item的view17 holder.price.setText(.);/赋值起作用18 .19 /textTotalPrice是最后一个Item中的view20 /与holder.price不是同一个item中的view21 textTotalPrice.setText(。);/赋值无效22 /更新list23 goods.get(position).setCount(count+);24 /更新数据:条用该方法的以后,会重新执行getView方法,非局部跟新25 GoodsListAdapter.this.notifyDataSetChanged();26 );完整代码如下:Java代码1. viewplaincopytoclipboardprint?2. importjava.util.List;3.4. importorg.android.util.NumberUtils;5.6. importandroid.app.Activity;7. importandroid.text.Editable;8. importandroid.text.TextWatcher;9. importandroid.util.Log;10. importandroid.view.LayoutInflater;11. importandroid.view.View;12. importandroid.view.ViewGroup;13. importandroid.widget.BaseAdapter;14. importandroid.widget.EditText;15. importandroid.widget.TextView;16.17. importcom.mmb.shop.R;18. /*19. *购物车:商品列表20. *21. *authorwangtao22. */23. publicclassGoodsListAdapterextendsBaseAdapter24.25. privatestaticListgoods;26.27. privateLayoutInflatermInflater;28.29. privatestaticTextViewtextTotalPrice;30.31. /privateActivitycontext;32.33. publicGoodsListAdapter(Listgoods_,Activitycontext)34. goods=goods_;35. mInflater=context.getLayoutInflater();36. /this.context=context;37. 38.39. Override40. publicViewgetView(finalintposition,ViewconvertView,finalViewGroupparent)41. /最后一条显示总价42. if(position=goods.size()43. convertView=mInflater.inflate(android.R.layout.simple_list_item_1,parent,false);44. textTotalPrice=(TextView)convertView.findViewById(android.R.id.text1);45. if(goods.size()0)46. textTotalPrice.setText(总价:+calcuteTotalPrice()+);47. else48. textTotalPrice.setText(购物车为空.);49. 50. returnconvertView;51. 52.53. finalViewHolderholder=newViewHolder();54. /商品列表布局55. convertView=mInflater.inflate(R.layout.list_item_shop_car,parent,false);56. holder.id=(TextView)convertView.findViewById(R.id.goods_id);57. =(TextView)convertView.findViewById(R.id.goods_name);58. /不能使用Holder模式;必须每次都创建一个不同的EditText组件59. holder.count=(EditText)convertView.findViewById(R.id.goods_count);60. /单价61. holder.singlePrice=(TextView)convertView.findViewById(R.id.goods_single_price);62. /总价63. holder.price=(TextView)convertView.findViewById(R.id.goods_price);64.65. finalGoodsitem=goods.get(position);66. /holder.id.setText(item.getId();67. .setText(item.getName();68. holder.count.setText(item.getCount();69. holder.singlePrice.setText(item.getSinglePrice();70. floattotalPrice=Integer.valueOf(item.getCount()*Float.valueOf(item.getSinglePrice();71. holder.price.setText(价格:+totalPrice+);72. /设置没类产品的总价73. goods.get(position).setTotalPrice(totalPrice+);74.75. /添加编辑框的change事件76. holder.count.addTextChangedListener(newTextWatcher()77. Override78. publicvoidonTextChanged(CharSequences,intstart,intbefore,intcount)79. 80. Override81. publicvoidbeforeTextChanged(CharSequences,intstart,intcount,82. intafter)83. 84. Override85. publicvoidafterTextChanged(Editables)86. try/s.toString()即是文本框的值87. intcount=Integer.valueOf(s.toString();88. floatsinglePrice=Integer.valueOf(item.getSinglePrice();89. floattotalPrice=count*singlePrice;90. holder.price.setText(totalPrice+);91. goods.get(position).setTotalPrice(totalPrice+);92. textTotalPrice.setText(GoodsListAdapter.calcuteTotalPrice()+);93. goods.get(position).setCount(count+);94. /更新数据95. GoodsListAdapter.this.notifyDataSetChanged();96. /ViewconvertView=mInflater.inflate(android.R.layout.simple_list_item_1,parent,false);97. /updateItemInTotalPrice();98. catch(Exceptione)99. Log.e(xx,e.getStackTrace().toString();100. 101.102. 103. );104. returnconvertView;105. 106. /ViewHolder模式?107. staticclassViewHolder108. TextViewid;/ID109. TextViewname;/名称110. EditTextcount;/数量111. TextViewsinglePrice;/单价112. TextViewprice;/单个商品的总价113.114. 115.116. /*117. *计算所有购物车商品总价118. *return119. */120. privatefinalstaticfloatcalcuteTotalPrice()121. floatprice=0f;122. for(Goodsgs:goods)123. price+=NumberUtils.toFloat(gs.getTotalPrice();124. 125. returnprice;126. 127. /更新购物车商品总价Item非全部整合ListView128. /privatefinalvoidupdateItemInTotalPrice()129. /TextViewview=(TextView)this.getItem(goods.size();130. /view.setText(ddddd

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论