4

I got this exception, i am new for android so please help me.

E/UncaughtException: java.lang.IndexOutOfBoundsException: setSpan (2 ... 2) ends beyond length 1

    holder.oldLaundriesQuantityEditText.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) {

            if(s.toString().length()>0){
                int editedQuantity = Integer.parseInt(s.toString());

                if(editedQuantity==0){

                    finalHolder.oldLaundriesQuantityEditText.setText("1");

                }
                if(editedQuantity>oldLaundriesModelArrayList.get(position).getRemain_quantity()){

                    finalHolder.oldLaundriesQuantityEditText.setText(String.valueOf(
                            oldLaundriesModelArrayList.get(position).getRemain_quantity()
                    ));

                }

                finalHolder.oldLaundriesQuantityEditText.setSelection(count);
            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
Hafijur Rahman
  • 53
  • 1
  • 1
  • 10

2 Answers2

6

This issue happens when you have already entered the text of length 2 say "10" but the remaining quantity returns a single digit one say "9", in that case the count value will be 2. And on setting selection you are using count value and there is a string of length 1 only but setting to 2.

Instead of

finalHolder.oldLaundriesQuantityEditText.setSelection(count);

use

finalHolder.oldLaundriesQuantityEditText.setSelection(finalHolder.oldLaundriesQuantityEditText.getText().toString().trim().length());

For a better and cleaner one, store the value of

String.valueOf(oldLaundriesModelArrayList.get(position).getRemain_quantity());

to a string before setting to the edittext and then use the same string object to get the length of the string.

arne.z
  • 3,242
  • 3
  • 24
  • 46
Dinash
  • 3,027
  • 4
  • 32
  • 45
  • code is working but on this screen the app not responding when i debug this than inside on "onTextChanged" debugging continue for infinite ...plz tell how to solve this ? – Hafijur Rahman Jul 18 '17 at 09:21
  • So, when we change text inside a text change listener, I runs into infinite loop as discussed on this link. https://stackoverflow.com/questions/9385081/how-can-i-change-the-edittext-text-without-triggering-the-text-watcher So simply use a boolean flag to avoid the infinite loop – Dinash Jul 18 '17 at 09:38
  • I have solved this kind of issue by this https://github.com/RedMadRobot/input-mask-android/issues/27 answer. – Ninja Jun 04 '18 at 09:40
1

check size this oldLaundriesModelArrayList

if(oldLaundriesModelArrayList.size() > 0 && position <= oldLaundriesModelArrayList.size() ){
    finalHolder.oldLaundriesQuantityEditText.setText(String.valueOf(oldLaundriesModelArrayList.get(position).getRemain_quantity())); 
}
Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78