Android: EditText in ListView issue

前端 未结 5 1081
梦如初夏
梦如初夏 2020-12-30 11:07

I have a listview in my application that is basically a questionaire so there are some EditTexts that the user needs to fill. I\'ve encountered the following issues:

5条回答
  •  渐次进展
    2020-12-30 11:34

    I have a listview with Edittext and when I input text in it, if before this, I scroll the listview, the adapter take some positions, not the selected.

    My adapter take various positions instead of my selection:

    etCantidad.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) {
             String cantidadTMP = etCantidad.getText().toString();
             int cantidad;
             try {
                  cantidad = Integer.parseInt(cantidadTMP);
                  item.setCantidad(cantidad);
                  Log.i("Name",""+item.getNombre());
                 }catch (Exception e){
                        cantidad = 0;
                 }
             Log.i("QuantityArrayAdapter",String.valueOf(cantidad));
         }});
    

    One Editext selection:

    09-25 09:22:10.458 12719-12719/? I/QuantityArrayAdapter: 1
    09-25 09:22:10.458 12719-12719/? I/QuantityArrayAdapter: 1
    09-25 09:22:10.458 12719-12719/? I/QuantityArrayAdapter: 1
    

    So, I added this method in to the onTouchListener to only gets the selection position:

    etCantidad.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                etCantidad.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) {
                        String cantidadTMP = etCantidad.getText().toString();
                        int cantidad;
                        try {
                            cantidad = Integer.parseInt(cantidadTMP);
                            item.setCantidad(cantidad);
                            Log.i("Name",""+item.getNombre());
                        }catch (Exception e){
                            cantidad = 0;
                        }
                        Log.i("QuantityArrayAdapter",String.valueOf(cantidad));
                    }
                });
                return false;
            }
        });
    

    And only take the item i want so, its works for me.

提交回复
热议问题