EditText in ListView is updated by onTextChanged when scrolling

前端 未结 3 1425
失恋的感觉
失恋的感觉 2021-02-09 09:06

I have been searching for an answer to this, but the solutions don\'t seem to work for me. I have a TextView and an EditText in a list item. I am trying to update the stored v

3条回答
  •  隐瞒了意图╮
    2021-02-09 09:22

    I've had that same problem, when I move the scrolling to my list up / down the text value of my editText changed,then my solution was that in my EditText add a setOnFocusChangeListener and within it add a addTextChangedListener but relationed whit other instance of the same editText.

    EditText etSumScans;
    
    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
             view = convertView;
    
            if (view == null)
                view = LayoutInflater.from(myContextItem).inflate(resourceLayoutItem, null);
    
            shipmentLinesDTO = myListItem.get(position);
    
            etSumScans = view.findViewById(R.id.etSumScans);
            etSumScans.setText(shipmentLinesDTO.getAttribute1() );
    
            etSumScans.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus){
                        EditText secondInstanceEditText= v.findViewById(R.id.etSumScans);
                        secondInstanceEditText.addTextChangedListener(new TextWatcher() {
                            @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) {
                                getItem(position).setAttribute1(s.toString());
                            }
                        });
                    }
                }
            });
        }
    
    

    With this I solved my problem. I hope helps.

提交回复
热议问题