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> goods; private LayoutInflater mInflater;  private static TextView textTotalPrice; // private Activity context;  public GoodsListAdapter(List<Goods> 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_item_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.calcuteTotalPrice()+"");     goods.get(position).setCount(count+"");     //更新数据     GoodsListAdapter.this.notifyDataSetChanged();     //View convertView = mInflater.inflate(android.R.layout.simple_list_item_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; }}

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/gruhgd/article/details/87543042