0

I'm using array adapter to implement a search view to search for hard coded elements

my code as follows

public class MainActivity extends AppCompatActivity {
ListView list;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.toolbar);
    list = (ListView) findViewById(R.id.list_view);
    EditText edittext = (EditText) findViewById(R.id.editText);
    edittext.addTextChangedListener(textWatcher);
    adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.row_item, COUNTRIES);
}
TextWatcher textWatcher = 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().equals(""))
            list.setAdapter(null);
        else {
            adapter.getFilter().filter(s.toString());
            adapter.notifyDataSetChanged();
            list.setAdapter(adapter);
//                adapter.clear();
            Log.v("HEY", adapter.getCount() + "");
        }
    }
    @Override
    public void afterTextChanged(Editable s) {
    }
};
private static final String[] COUNTRIES = new String[]{
        "FAAAFA", "France", "FOO"};
}

When i type F in the search for example, the count is printed 0 instead of 3 then when i apply any other query the count is 3 (ie it prints the count of previous query to current one)

3
  • How does the filter method look like? Commented Jul 20, 2017 at 13:31
  • i just use adapter.getFilter().filter(s.toString()); Commented Jul 20, 2017 at 13:34
  • Check this answer stackoverflow.com/questions/10532194/… Commented Jul 20, 2017 at 13:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.