实现像Instagram这样的标签功能?

时间:2017-06-27 12:03:51

标签: android android-recyclerview

在这个演示中,我想添加像Instagram这样的标签功能。当我使用@正确搜索关注者并显示该关注者的名字时。但是当我在搜索中删除@时,它会显示错误。 java.lang.StringIndexOutOfBoundsException: length=0; index=1.我使用了很多代码但没有求解。需要帮助代码在这里: -

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);
            }
        });
    }

}

2 个答案:

答案 0 :(得分:2)

此功能出现错误。你需要在处理之前检查可编辑的长度。

@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);

                }
            }

当字符串为空时,您正在执行s.substring

所以添加如下

            @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);

                }
            }

答案 1 :(得分:0)

我认为你应该在完成逻辑之前检查长度。

    @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);
            }
         }
    }