Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to search in a ListView via ArrayAdapter?

    • 0
    • 3
    • 0
    • 0
    • 3
    • 0
    • 0
    • 0
    • 734
    Comment on it

    ListView with searching feature is very common in applications. You just need an EditText at the top of the ListView or as per your designs.
    Below are the steps-


    First you need to initialize the ListView and EditText

    countryCodelistView = (ListView) findViewById(R.id.country_code_list_view);
    countryCodeSearchEditText = (EditText) findViewById(R.id.country_code_search);
    


    Now add ArrayAdapter in a ListView

    adapter = new ArrayAdapter<String>(this, R.layout.country_code_list_item,
                    R.id.country_code_text_view, array);
    countryCodelistView.setAdapter(adapter);
    


    Here array is the list that holds by a ListView


    Now final step is to apply addTextChangedListener on the EditText

    countryCodeSearchEditText.addTextChangedListener(new TextWatcher() {
    
                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2,
                        int arg3) {
                    // When user changed the Text
                    CountryCodeActivity.this.adapter.getFilter().filter(cs);
                }
    
                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                        int arg2, int arg3) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                }
            });
    

 3 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: