我想下面的过滤是区分大小写的,即当我写ABC时它不会显示aBc等

时间:2017-01-19 08:57:31

标签: android

在下面的代码中,过滤不区分大小写。我希望过滤是区分大小写的,例如当我在我的应用程序中编写ABC时,它不会显示aBc,Abc,aBc,abC和abc:

private ListView lv;

ArrayAdapter<String> adapter;

EditText inputSearch;


ArrayList<HashMap<String, String>> productList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String products[] = {"ABC", "abc", "aBc", "Abc", "abC"

};

    lv = (ListView) findViewById(R.id.list_view);
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
    lv.setAdapter(adapter);

    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            MainActivity.this.adapter.getFilter().filter(cs);   
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {

        }

        @Override
        public void afterTextChanged(Editable arg0) {
        }
    });
}    

}

1 个答案:

答案 0 :(得分:0)

//You can try this 

inputSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            MainActivity.this.adapter.getFilter().filter(cs.toLowerCase(Locale.getDefault()));   
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {

        }

        @Override
        public void afterTextChanged(Editable arg0) {
        }
    });