0

In this demo I want to add tag functionality like Instagram. when I search a follower using @ its work properly and show name of that follower. but when I erase @ in search it shows error. java.lang.StringIndexOutOfBoundsException: length=0; index=1. I use many code but not solve.need help Code is here:-

public class Main2Activity extends AppCompatActivity {

    RecyclerView following_userr_list, mListView_COmment;
    EditText editTextSearch;
    ArrayList<String> FollowingListValuesArr;
    ArrayList<String> show_following;
    CustomAdapter adapter;
    Custom_comment_Adapter adapter1;
    String final_string = "";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        FollowingListValuesArr = new ArrayList<>();

        FollowingListValuesArr.add("Ramiz");
        FollowingListValuesArr.add("Karan");
        FollowingListValuesArr.add("Azad");
        FollowingListValuesArr.add("Manish");

        show_following = new ArrayList<>();

        show_following.add("Ramiz");
        show_following.add("Karan");
        show_following.add("Azad");
        show_following.add("Manish");

        following_userr_list = (RecyclerView) findViewById(recyclerView);
        editTextSearch = (EditText) findViewById(R.id.editTextSearch);
        mListView_COmment = (RecyclerView) findViewById(recyclerView_comment);


        following_userr_list.setHasFixedSize(true);
        following_userr_list.setLayoutManager(new LinearLayoutManager(this));
        mListView_COmment.setHasFixedSize(true);
        mListView_COmment.setLayoutManager(new LinearLayoutManager(this));

        adapter = new CustomAdapter(FollowingListValuesArr);
        adapter1 = new Custom_comment_Adapter(show_following);

        following_userr_list.setAdapter(adapter);
        mListView_COmment.setAdapter(adapter1);

        editTextSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int count) {


            }

            @Override
            public void afterTextChanged(Editable editable) {


                String s = editable.toString();

                final_string = s.substring(1);

                if (final_string.length() >= 1) {
                    following_userr_list.setVisibility(View.INVISIBLE);
                    mListView_COmment.setVisibility(View.VISIBLE);
                    filters(final_string);

                }
            }


            private void filters(String text) {
                ArrayList<String> filterdNames = new ArrayList<>();


                for (String s : FollowingListValuesArr) {

                    if (s.toLowerCase().contains(text.toLowerCase())) {

                        filterdNames.add(s);
                    }
                }

                adapter.filterList(filterdNames);
                adapter1.filterList(filterdNames);
            }
        });
    }

}
Anil
  • 1,605
  • 1
  • 14
  • 24
Navjot.jassal
  • 749
  • 2
  • 10
  • 29
  • Possible duplicate of [How to avoid ArrayIndexOutOfBoundsException or IndexOutOfBoundsException?](https://stackoverflow.com/questions/32568261/how-to-avoid-arrayindexoutofboundsexception-or-indexoutofboundsexception) – rushi Jun 27 '17 at 12:25

2 Answers2

2

You are getting error in this function. you need to check length of editable before process it.

@Override
            public void afterTextChanged(Editable editable) {


                String s = editable.toString();

                final_string = s.substring(1);// when s is null 

                if (final_string.length() >= 1) {
                    following_userr_list.setVisibility(View.INVISIBLE);
                    mListView_COmment.setVisibility(View.VISIBLE);
                    filters(final_string);

                }
            }

When string is null you are doing s.substring

So add like below

            @Override
            public void afterTextChanged(Editable editable) {

               if (editable.length() > 0) {
                String s = editable.toString();

                final_string = s.substring(1);


                    following_userr_list.setVisibility(View.INVISIBLE);
                    mListView_COmment.setVisibility(View.VISIBLE);
                    filters(final_string);

                }
            }
Anil
  • 1,605
  • 1
  • 14
  • 24
0

I think You should check the length before going through your logic.

    @Override
    public void afterTextChanged(Editable editable) {
        if(editable.length()>0){
            String s = editable.toString();
            final_string = s.substring(1);
            if (final_string.length() >= 1) {
                following_userr_list.setVisibility(View.INVISIBLE);
                mListView_COmment.setVisibility(View.VISIBLE);
                filters(final_string);
            }
         }
    }
The Dongster
  • 337
  • 1
  • 3
  • 15