listview监听组件内容变化

package com.meizu.ui.gifts;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
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.meizu.R;
import com.meizu.ui.sales.GiftsGivingBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by laoyimou on 2018/3/15.
 */

public class GiftEditAdapter extends BaseAdapter {
    private GiftAuditDetailBean detailsBean = new GiftAuditDetailBean();
    List<GiftAuditDetailBean> list ;
    private Context context = null;
    private LayoutInflater inflater = null;

    public GiftEditAdapter(List<GiftAuditDetailBean> list, Context context) {
        this.list = list;
        this.context = context;
        // 布局装载器对象
        inflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return list.size();
    }

    // 获取数据集中与指定索引对应的数据项
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    // 获取指定行对应的ID
    @Override
    public long getItemId(int position) {
        return position;
    }

    // 获取每一个Item显示的内容
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder(parent, context);
        } else {
            viewHolder = (ViewHolder) convertView.getTag(); // 获取,通过ViewHolder找到相应的控件
        }
        detailsBean = list.get(position);
        viewHolder.updateView(detailsBean);
        return viewHolder.getContentView();
    }


    private class ViewHolder implements TextWatcher {

        private View mContentView;
        private TextView giftCode;
        private TextView reduce;
        private EditText getSum;
        private TextView add;
        private TextView giftsName;
        private GiftAuditDetailBean getBean;

        public ViewHolder(ViewGroup parent, Context context) {
            mContentView = inflater.inflate(R.layout.gifts_edit_item, parent, false);

            mContentView.setTag(this);
            giftsName = mContentView.findViewById(R.id.giftsName);
            giftCode = mContentView.findViewById(R.id.giftCode);
            reduce = mContentView.findViewById(R.id.reduce);
            add = mContentView.findViewById(R.id.add);
            getSum = mContentView.findViewById(R.id.getSum);
            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //由于我们已经限制了只能输入整数,所以只需要判断首个字符是不是0,或者内容为空即可
                    if (getSum.getText().toString() == null || "".equals(getSum.getText().toString())) {
                        getSum.setText("0");
                    }
                    int a = Integer.parseInt(getSum.getText().toString());
                    a = a + 1;
                    getSum.setText(a +"");
                }
            });

            reduce.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //由于我们已经限制了只能输入整数,所以只需要判断首个字符是不是0,或者内容为空即可
                    if (getSum.getText().toString() == null || "".equals(getSum.getText().toString())) {
                        getSum.setText("0");

                    }
                    if (Integer.parseInt(getSum.getText().toString()) == 0) {
                        return;
                    } else {
                        //拿出内容并进行计算
                        int a = Integer.parseInt(getSum.getText().toString()) - 1;
                        //计算之后再设置回去
                        getSum.setText(a + "");
                    }

                }
            });
            // 监听EditText的输入变化,然后在对应的回调中记录输入的值
            getSum.addTextChangedListener(this);
        }

        public void updateView(GiftAuditDetailBean detailBean) {
            getBean = detailBean;
            giftsName.setText(getBean.getFmaterrialName());
            getSum.setText("" + getBean.getFdownNum());
            giftCode.setText("" + getBean.getFmaterrialCode());

        }

        public View getContentView() {
            return mContentView;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }
        @Override
        public void afterTextChanged(Editable s) {
            // 输入内容时,记录下来,放在bean中暂存,这样就可以随时通过bean获取
            getBean.setFdownNum(Integer.parseInt(s.toString()));
        }
    }
}

 

posted @ 2018-07-05 16:34  劳猿外  阅读(939)  评论(0编辑  收藏  举报