作者:super---小杰_360 | 来源:互联网 | 2023-08-19 11:51
部分代码如下:其中holder.count是一个EditViewholder.price是一个TextView复制到剪贴板Java代码viewplaincopytoclipboar
// 部分代码如下:
其中holder.count 是一个EditView
holder.price 是一个TextView
Java代码
- view plaincopy to clipboardprint?
- @Override
- public View getView(final int position,View convertView,final ViewGroup parent) {
- 。。。。。。
-
- 导致监听不到指定item中的view的事件,解决办法就是每次创建一个Item中的组件
-
- 然后对于每个item 使用不同的监听事件 即 new TextWatcher() 每次都创建一个新的事件监听器
-
- final ViewHolder holder = new ViewHolder();
-
- holder.count.addTextChangedListener( new TextWatcher() {
- .....
- @Override
- public void afterTextChanged(Editable s) {
-
- holder.price.setText(".......");
- .....
-
-
- textTotalPrice.setText("。。。。");
-
- goods.get(position).setCount(count+"");
-
- GoodsListAdapter.this.notifyDataSetChanged();
- });
完整代码如下:
复制到剪贴板
Java代码
-
- view plaincopy to clipboardprint?
- 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;
-
-
-
-
-
- public class GoodsListAdapter extends BaseAdapter {
-
- private static List goods;
-
- private LayoutInflater mInflater;
-
- private static TextView textTotalPrice;
-
-
-
- public GoodsListAdapter(List goods_, Activity context) {
- goods = goods_;
- mInflater = context.getLayoutInflater();
-
- }
-
- @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.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.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 + "");
-
-
- 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{
- 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();
-
-
- }catch(Exception e){
- Log.e("xx", e.getStackTrace().toString());
- }
-
- }
- });
- return convertView;
- }
-
- static class ViewHolder {
- TextView id;
- TextView name;
- EditText count;
- TextView singlePrice;
- TextView price;
-
- }
-
-
-
-
-
- private final static float calcuteTotalPrice(){
- float price = 0f;
- for(Goods gs : goods){
- price += NumberUtils.toFloat(gs.getTotalPrice());
- }
- return price;
- }
-
-
-
-
-
-
- @Override
- public int getCount() {
- return goods.size() + 1;
- }
- @Override
- public Object getItem(int position) {
- return position;
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
-
- }