Android: EditText in ListView issue

前端 未结 5 1065
梦如初夏
梦如初夏 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:18

    To expand on aaRBiyecH's answer above, here is a solution and a more in-depth explanation to your questions #1 and #3 (source):

    You need to set the DescendantFocusability property of your ListView to "afterDescendants".

    You can do this in the XML like this:

    android:descendantFocusability="afterDescendants"

    or in code like this:

    listview.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS)

    You also need to set the items to be focusable.

    You can do this in the code like this: listview.setItemsCanFocus(true);

    Also, you need to specify what is happening to the main window of your activity when the soft keyboard is shown. This is done by changing the WindowSoftInputMode attribute. You probably want to set it to AdjustPan so that your listview is not resized and thus GetView isn't called again. This is probably what is happening now when you hide the keyboard, GetView is called again and reset the original value in the EditText.

    You can do this using the attribute android:windowSoftInputMode="adjustPan" on your main activity class's XML, or getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_PAN) programmatically.

    0 讨论(0)
  • 2020-12-30 11:19

    due to question 2 you can relate to this post EditText items in a scrolling list lose their changes when scrolled off the screen so there seems there s no easy solution to persist the edittexts when you scroll down

    Yes your right you can try

    EditText loses content on scroll in ListView

    or just relate to the related posts on the right

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-30 11:40

    Solutions to issue #3:

    1. Set descendantFocusablity="beforeDescendants" in the listview.

    2. Set windowSoftInputMode="adjustPan" in the manifest

    3. Set listview.setItemsCanFocus(true)

    0 讨论(0)
  • 2020-12-30 11:42

    Its working for me.... Please check this

    listview.setItemsCanFocus(true);
    
    0 讨论(0)
提交回复
热议问题