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

  // 部分代码如下:
  其中holder.count 是一个EditView
  holder.price 是一个TextView @Override public View getView(final int position,View convertView,final ViewGroup parent) { 。。。。。。 // 注意该方法中的Item组件不能使用holder模式,如果用该模式, 所有的组件将共享item中view的事件 导致监听不到指定item中的view的事件,解决办法就是每次创建一个Item中的组件 然后对于每个item 使用不同的监听事件即 new TextWatcher() 每次都创建一个新的事件监听器 final ViewHolder holder = new ViewHolder(); holder.count.addTextChangedListener( new TextWatcher() { ..... @Override public void afterTextChanged(Editable s) { //holder.price 是与holder.count在同一个item的view holder.price.setText("......."); //赋值起作用 ..... //textTotalPrice是最后一个Item中的view //与holder.price 不是同一个item中的view textTotalPrice.setText("。。。。");//赋值无效 // 更新list goods.get(position).setCount(count+""); //更新数据:条用该方法的以后, 会重新执行getView方法,非局部跟新 GoodsListAdapter.this.notifyDataSetChanged(); }); 完整代码如下: import java.util.List; import org.android.util.NumberUtils; import android.app.Activity; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.TextView; import com.mmb.shop.R; /** * 购物车:商品列表 * * @author wangtao */ public class GoodsListAdapter extends BaseAdapter { private static List goods; private LayoutInflater mInflater; private static TextView textTotalPrice; // private Activity context; public GoodsListAdapter(List goods_, Activity context) { goods = goods_; mInflater = context.getLayoutInflater(); // this.context = context; } @Override public View getView(final int position,View convertView,final ViewGroup parent) { //最后一条显示总价 if(position == goods.size()){ convertView = mInflater.inflate(android.R.layout.simple_list_ite m_1, parent, false); textTotalPrice = (TextView) convertView.findViewById(android.R.id.text1); if(goods.size() > 0){ textTotalPrice.setText("总价: "+calcuteTotalPrice()+""); }else{ textTotalPrice.setText("购物车为空...."); } return convertView; } final ViewHolder holder = new ViewHolder(); //商品列表布局 convertView = mInflater.inflate(R.layout.list_item_shop_car, parent, false); holder.id = (TextView) convertView.findViewById(R.id.goods_id); holder.name = (TextView) convertView.findViewById(R.id.goods_name); //不能使用Holder模式; 必须每次都创建一个不同的EditText组件 holder.count = (EditText) convertView.findViewById(R.id.goods_count); //单价 holder.singlePrice = (TextView) convertView.findViewById(R.id.goods_single_price); //总价 holder.price = (TextView) convertView.findViewById(R.id.goods_price); final Goods item = goods.get(position); //holder.id.setText(item.getId()); holder.name.setText(item.getName()); holder.count.setText(item.getCount()); holder.singlePrice.setText(item.getSinglePrice()); float totalPrice = Integer.valueOf(item.getCount()) * Float.valueOf(item.getSinglePrice()); holder.price.setText("价格: " + totalPrice + ""); //设置没类产品的总价 goods.get(position).setTotalPrice(totalPrice + ""); //添加编辑框的change事件 holder.count.addTextChangedListener( new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { try{//s.toString() 即是 文本框的值 int count = Integer.valueOf(s.toString()); float singlePrice = Integer.valueOf(item.getSinglePrice()); float totalPrice = count * singlePrice; holder.price.setText(totalPrice + ""); goods.get(position).setTotalPrice(totalPrice + ""); textTotalPrice.setText(GoodsListAdapter.calcuteTot alPrice()+""); goods.get(position).setCount(count+""); //更新数据 GoodsListAdapter.this.notifyDataSetChanged(); //View convertView = mInflater.inflate(android.R.layout.simple_list_ite m_1, parent, false); //updateItemInTotalPrice(); }catch(Exception e){ Log.e("xx", e.getStackTrace().toString()); } } }); return convertView; } // ViewHolder模式???? static class ViewHolder { TextView id; // ID TextView name; // 名称 EditText count; // 数量 TextView singlePrice;//单价 TextView price; // 单个商品的总价 } /** * 计算所有购物车商品总价 * @return */ private final static float calcuteTotalPrice(){ float price = 0f; for(Goods gs : goods){ price += NumberUtils.toFloat(gs.getTotalPrice()); } return price; } //更新购物车商品总价Item ~~ 非全部整合ListView // private final void updateItemInTotalPrice(){ // TextView view = (TextView) this.getItem(goods.size()); // view.setText("ddddddddd"); // } @Override public int getCount() { return goods.size() + 1; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值