-2

How can I get a new ArrayList<String> of results after filtering an ArrayAdapter<String> using a TextWatcher in android?

The following is my code for filtering an ArrayAdapter<String> using the text from an EditText variable named edtSearch. The search function is working well. I need to put the filtered data into a new ArrayList<String>:

edtSearch.addTextChangedListener(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) {
        MainActivity.this.adapter.getFilter().filter(s);
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});
George Mulligan
  • 11,813
  • 6
  • 37
  • 50

1 Answers1

1

You can loop through your adapter and add the results remaining to a new List:

List<String> results = new ArrayList<String>();
for(int i = 0; i < adapter.getCount(); i++) {
    results.add(adapter.getItem(i));
}
George Mulligan
  • 11,813
  • 6
  • 37
  • 50