android 为 ListView Item中的组件添加事件 以及更新数据

// 部分代码如下:

其中holder.count 是一个EditView

holder.price 是一个TextView

Java代码
  1. view plaincopy to clipboardprint?    
  2. @Override      
  3.     public View getView(final int position,View convertView,final ViewGroup parent) {      
  4. 。。。。。。      
  5. // 注意该方法中的Item组件不能使用holder模式,如果用该模式, 所有的组件将共享item中view的事件      
  6. 导致监听不到指定item中的view的事件,解决办法就是每次创建一个Item中的组件      
  7.       
  8. 然后对于每个item 使用不同的监听事件 即 new TextWatcher() 每次都创建一个新的事件监听器      
  9.       
  10. final ViewHolder holder = new ViewHolder();      
  11.       
  12. holder.count.addTextChangedListener( new TextWatcher() {      
  13.             .....      
  14.     @Override      
  15.     public void afterTextChanged(Editable s) {      
  16.     //holder.price 是与holder.count在同一个item的view                    
  17. holder.price.setText("......."); //赋值起作用      
  18.          .....      
  19.         //textTotalPrice是最后一个Item中的view      
  20.         //与holder.price 不是同一个item中的view      
  21.     textTotalPrice.setText("。。。。");//赋值无效      
  22.          // 更新list      
  23.     goods.get(position).setCount(count+"");      
  24.     //更新数据:条用该方法的以后, 会重新执行getView方法,非局部跟新      
  25.     GoodsListAdapter.this.notifyDataSetChanged();      
  26. });      

完整代码如下:   复制到剪贴板  

Java代码
  1.  
  2. view plaincopy to clipboardprint?  
  3. import java.util.List;    
  4.     
  5. import org.android.util.NumberUtils;    
  6.     
  7. import android.app.Activity;    
  8. import android.text.Editable;    
  9. import android.text.TextWatcher;    
  10. import android.util.Log;    
  11. import android.view.LayoutInflater;    
  12. import android.view.View;    
  13. import android.view.ViewGroup;    
  14. import android.widget.BaseAdapter;    
  15. import android.widget.EditText;    
  16. import android.widget.TextView;    
  17.     
  18. import com.mmb.shop.R;    
  19. /**  
  20.  * 购物车:商品列表  
  21.  *   
  22.  * @author wangtao  
  23.  */    
  24. public class GoodsListAdapter extends BaseAdapter {    
  25.         
  26.     private static List<Goods> goods;    
  27.     
  28.     private LayoutInflater mInflater;    
  29.         
  30.     private static TextView textTotalPrice;    
  31.         
  32. //  private Activity context;    
  33.         
  34.     public GoodsListAdapter(List<Goods> goods_, Activity context) {    
  35.         goods = goods_;    
  36.         mInflater = context.getLayoutInflater();    
  37. //      this.context = context;    
  38.     }    
  39.     
  40.     @Override    
  41.     public View getView(final int position,View convertView,final ViewGroup parent) {    
  42.         //最后一条显示总价    
  43.         if(position == goods.size()){    
  44.             convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);    
  45.             textTotalPrice = (TextView) convertView.findViewById(android.R.id.text1);    
  46.             if(goods.size() > 0){    
  47.                 textTotalPrice.setText("总价: "+calcuteTotalPrice()+"");    
  48.             }else{    
  49.                 textTotalPrice.setText("购物车为空....");    
  50.             }    
  51.             return convertView;    
  52.         }    
  53.             
  54.         final ViewHolder holder = new ViewHolder();    
  55.         //商品列表布局    
  56.         convertView = mInflater.inflate(R.layout.list_item_shop_car, parent, false);    
  57.         holder.id = (TextView) convertView.findViewById(R.id.goods_id);    
  58.         holder.name = (TextView) convertView.findViewById(R.id.goods_name);    
  59.         //不能使用Holder模式; 必须每次都创建一个不同的EditText组件    
  60.         holder.count = (EditText) convertView.findViewById(R.id.goods_count);    
  61.         //单价    
  62.         holder.singlePrice = (TextView) convertView.findViewById(R.id.goods_single_price);    
  63.         //总价    
  64.         holder.price = (TextView) convertView.findViewById(R.id.goods_price);    
  65.             
  66.         final Goods item = goods.get(position);    
  67.         //holder.id.setText(item.getId());    
  68.         holder.name.setText(item.getName());    
  69.         holder.count.setText(item.getCount());    
  70.         holder.singlePrice.setText(item.getSinglePrice());    
  71.         float totalPrice = Integer.valueOf(item.getCount()) * Float.valueOf(item.getSinglePrice());    
  72.         holder.price.setText("价格: " + totalPrice + "");    
  73.         //设置没类产品的总价    
  74.         goods.get(position).setTotalPrice(totalPrice + "");    
  75.             
  76.         //添加编辑框的change事件    
  77.         holder.count.addTextChangedListener( new TextWatcher() {    
  78.             @Override    
  79.             public void onTextChanged(CharSequence s, int start, int before, int count) {    
  80.             }    
  81.             @Override    
  82.             public void beforeTextChanged(CharSequence s, int start, int count,    
  83.                     int after) {    
  84.             }    
  85.             @Override    
  86.             public void afterTextChanged(Editable s) {    
  87.                 try{//s.toString() 即是 文本框的值    
  88.                     int count = Integer.valueOf(s.toString());    
  89.                     float singlePrice = Integer.valueOf(item.getSinglePrice());    
  90.                     float totalPrice = count * singlePrice;    
  91.                     holder.price.setText(totalPrice + "");    
  92.                     goods.get(position).setTotalPrice(totalPrice + "");    
  93.                     textTotalPrice.setText(GoodsListAdapter.calcuteTotalPrice()+"");    
  94.                     goods.get(position).setCount(count+"");    
  95.                     //更新数据    
  96.                     GoodsListAdapter.this.notifyDataSetChanged();    
  97.                     //View convertView = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);    
  98.                     //updateItemInTotalPrice();    
  99.                 }catch(Exception e){    
  100.                     Log.e("xx", e.getStackTrace().toString());    
  101.                 }    
  102.                     
  103.             }    
  104.         });    
  105.         return convertView;    
  106.     }    
  107.     // ViewHolder模式????    
  108.     static class ViewHolder {    
  109.         TextView id;    // ID    
  110.         TextView name;  // 名称    
  111.         EditText count; // 数量    
  112.         TextView singlePrice;//单价    
  113.         TextView price; // 单个商品的总价    
  114.             
  115.     }    
  116.         
  117.     /**  
  118.      * 计算所有购物车商品总价  
  119.      * @return  
  120.      */    
  121.     private final static float calcuteTotalPrice(){    
  122.         float price = 0f;    
  123.         for(Goods gs : goods){    
  124.             price += NumberUtils.toFloat(gs.getTotalPrice());    
  125.         }    
  126.         return price;    
  127.     }    
  128.     //更新购物车商品总价Item ~~ 非全部整合ListView    
  129. //  private final void updateItemInTotalPrice(){    
  130. //      TextView view = (TextView) this.getItem(goods.size());    
  131. //      view.setText("ddddddddd");    
  132. //  }    
  133.         
  134.     @Override    
  135.     public int getCount() {    
  136.         return goods.size() + 1;    
  137.     }    
  138.     @Override    
  139.     public Object getItem(int position) {    
  140.         return position;    
  141.     }    
  142.     @Override    
  143.     public long getItemId(int position) {    
  144.         return position;    
  145.     }    
  146.     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值