滚动丢失数据时,Listview中的Edittext

时间:2017-06-19 10:04:46

标签: android listview

我尝试使用edittext制作listview for。但我的apdater没有按预期工作。当我向下滚动然后向上滚动时,第一个editext中的值将丢失。 我的笨蛋:

public class ArrayAdapterListProduct extends BaseAdapter implements Filterable {

    private ArrayList<ProductItem> listProductItems = new ArrayList<>();
    private ArrayList<ProductItem> mOriginalValues = new ArrayList<>();
    private Context context = null;
    private Activity a = null;

    static class ViewHolder {
        TextView text;
        TextView timestamp;
        ImageView icon;
        EditText caption;
        ProgressBar progress;
        int position;
    }

    public ArrayAdapterListProduct(Activity a, Context context, ArrayList<ProductItem> productItems) {
        this.listProductItems = productItems;
        this.mOriginalValues = productItems;
        this.context = context;
        this.a = a;
    }
    public void swapItem(ArrayList<ProductItem> productItems) {
        this.listProductItems = productItems;
        notifyDataSetChanged();
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(this.context);
            view = inflater.inflate(R.layout.layout_lv_item, null);
            holder.caption = (EditText) view.findViewById(R.id.earnpoit_listview_item_number);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.caption = (EditText) view.findViewById(R.id.earnpoit_listview_item_number);
        view.setTag(holder);
        if (position % 2 != 0) {
            view.setBackgroundColor(parent.getResources().getColor(R.color.earnpoint_color_bg_item_lv));
        } else {
            view.setBackgroundColor(parent.getResources().getColor(R.color.WHITE));
        }
        final ProductItem productItem = listProductItems.get(position);

        TextView txt_name = (TextView) view.findViewById(R.id.earnpoit_listview_item_productname);
        TextView txt_exist = (TextView) view.findViewById(R.id.earnpoit_listview_item_exist);
        txt_name.setText(productItem.getName());
        txt_exist.setText(productItem.getQuantity() + "");

        holder.caption.setText(productItem.getNumber_choose() + "");
        holder.caption.setId(position);

        holder.caption.addTextChangedListener(new MyTextWatcher(holder.caption));

        holder.caption.setFilters(new InputFilter[]{new InputFilterMinMax(0, Integer.parseInt(txt_exist.getText().toString()))});
        return view;
    }

    private class MyTextWatcher implements TextWatcher {

        private EditText editText;

        private MyTextWatcher(EditText editText) {
            this.editText = editText;
        }

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

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            final int position = this.editText.getId();
            final EditText Caption = (EditText) this.editText;
            listProductItems.get(position).setNumber_choose(Integer.parseInt(Caption.getText().toString()));
            TextView txt_point_to_product = (TextView) a.findViewById(R.id.sellout_txt_point_in_order);
            Double total_point = 0.0;
            for (int i = 0; i < listProductItems.size(); i++) {
                ProductItem productItem1 = listProductItems.get(i);
                if (productItem1.getNumber_choose() > 0) {
                    total_point = total_point + productItem1.getNumber_choose()*productItem1.getPoint();
                }
            }
            txt_point_to_product.setText(total_point + "");
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                ArrayList<ProductItem> filterArray = new ArrayList<ProductItem>();

                if (mOriginalValues == null) {
                    mOriginalValues = new ArrayList<>(listProductItems);
                }

                if (constraint == null || constraint.length() == 0) {
                    results.count = mOriginalValues.size();
                    results.values = mOriginalValues;
                } else {
                    constraint = constraint.toString().toLowerCase();
                    for (int i = 0; i < mOriginalValues.size(); i++) {
                        String data = mOriginalValues.get(i).getName();
                        if (data.toLowerCase().startsWith(constraint.toString())) {
                            filterArray.add(mOriginalValues.get(i));
                        }
                    }
                    // set the Filtered result to return
                    results.count = filterArray.size();
                    results.values = filterArray;
                }
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                listProductItems = (ArrayList<ProductItem>) results.values;
                notifyDataSetChanged();
            }
        };
        return filter;
    }
}

请帮帮我。谢谢你!

1 个答案:

答案 0 :(得分:0)

请参阅以下

 holder.edtitext.setTag(position);   // add this line

    holder.edtitext.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            int pos = (Integer) holder.edtitext.getTag();
           ProductItem productItem1 = listProductItems.get(pos);

           // do your work
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });