在 Android TextWatcher 中汇总 EditText 值

图片

我有一个RecyclerView动态创建的字段。为了保存存储的数据,我使用了 TextWatcher。但我不知道如何总结 EditText -Cost 中的值并在屏幕底部的 TextView 中显示该值

public class ItemsAdapter extends

ListAdapter<Item,ItemsAdapter.ViewHolder> {

        private List<Item> mItems=new ArrayList<>();


        public static final DiffUtil.ItemCallback<Item>DIFF_CALLBACK=

                new DiffUtil.ItemCallback<Item>() {

                    @Override

                    public boolean areItemsTheSame( Item oldItem, Item newItem) {

                        return oldItem.getId()==newItem.getId();

                    }


                    @Override

                    public boolean areContentsTheSame(Item oldItem, Item newItem) {

                        return(oldItem.getName()==newItem.getName()&&oldItem.getCost()==newItem.getCost());

                    }

                };


        public ItemsAdapter(){super(DIFF_CALLBACK);}

        public void addMorreItems(List<Item> newItems){

            int insertionPosition=mItems.size();

            mItems.addAll(newItems);

            notifyItemRangeInserted(insertionPosition,newItems.size());

            submitList(mItems);


        }

        @Override

        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){

            Context context=parent.getContext();

            LayoutInflater inflater=LayoutInflater.from(context);


            View itemView=inflater.inflate(R.layout.custom_edittext_layout,parent,false);

            ViewHolder viewHolder=new ViewHolder(itemView);

            return viewHolder;


        }

        @Override

        public void onBindViewHolder(ViewHolder viewHolder,int position){

            viewHolder.editTextName.setText(mItems.get(position).getName());

            viewHolder.editTextCost3.setText(mItems.get(position).getCost());


            Item item=getItem(position);

            EditText editText=viewHolder.editTextName;

            editText.setText(item.getName());

            EditText editText1=viewHolder.editTextCost3;

            editText1.setText(item.getCost());

        }


慕运维8079593
浏览 109回答 1
1回答

www说

为持有 TextView的Activity/添加回调以获取总成本:Fragmentinterface OnCostChangeListener {&nbsp; void onChange(List<Item> list);}实现它:class MyActivity extends AppCompatActivity implements OnCostChangeListener {//...&nbsp; @Override&nbsp;&nbsp;&nbsp; public void onChange(List<Item> list) {&nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; for (Item it : list) {&nbsp; &nbsp; &nbsp; sum += it.getCost();&nbsp; &nbsp; }&nbsp; &nbsp; ((TextView) findViewById(R.id.totalCost)).setText(String.valueOf(sum));&nbsp; }//...}订阅适配器:private OnCostChangeListener mListener;public ItemsAdapter(OnCostChangeListener listener){&nbsp; super(DIFF_CALLBACK);&nbsp; this.mListener = listener;}并在以下位置使用侦听器TextWatcher:editTextCost3.addTextChangedListener(new TextWatcher() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void beforeTextChanged(CharSequence s, int start, int count, int after) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onTextChanged(CharSequence s, int start, int before, int count) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mItems.get(getAdapterPosition()).setmCost(editTextCost3.getText().toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mListener != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mListener.onChange(mItems);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void afterTextChanged(Editable s) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });升级版。它应该cost存储Item为数字(整数,在示例中),而不是字符串
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java