1

My code below is for search filter for a list view. Every time the text in the tbSearch editText is changed, the items in the listview must be changed. The execution goes inside the if statement (txt.length()==0) but it does not add my array.

public void onTextChanged(CharSequence s, int start, int before, int count) {
    String txt = tbSearch.getText().toString().toLowerCase();
    aaItems.clear();
    if (txt.length() == 0)
    {
        aaItems.addAll(arrMonth);
    }
    else {
        for (String item : arrMonth) {
            if (item.toLowerCase().contains(txt)) {
                aaItems.add(item);
            }
        }
    }
}
Makoto
  • 104,088
  • 27
  • 192
  • 230

2 Answers2

2

The List you are passing to your ArrayAdapter is used as the backing ArrayList for the adapter.

When you call clear(), you are literally clearing the backing array you gave it.

You'll need to ensure that the list you give the adapter is different than your arrMonth list like so:

aaItems = new ArrayAdapter(this, android.R.layout.simple_list_item_1, 
    new ArrayList<String>(arrMonths));
Makoto
  • 104,088
  • 27
  • 192
  • 230
Michael Krause
  • 4,689
  • 1
  • 21
  • 25
0

Okay, I solved the problem with the help of @Michael Krause. Thanks bro! I copied another arraylist which is temp, and used in inside the textchangedlistener.

public class ItemList extends Activity {

    ListView lvItems;
    EditText tbSearch;
    //String[] arrMonth = {"January","February","March","April","May","June","July","August","September","October","November","December"};
    ArrayList<String> arrMonth = new ArrayList<>();
    ArrayList<String> temp = new ArrayList<>();
    ArrayAdapter<String> aaItems;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_list);
        lvItems = (ListView) findViewById(R.id.lvItems);
        tbSearch = (EditText) findViewById(R.id.tbSearch);
        PopulateArrayList();
        temp.addAll(arrMonth);

        aaItems = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrMonth);
        lvItems.setAdapter(aaItems);

        tbSearch.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) {
                String txt = tbSearch.getText().toString().toLowerCase();
                aaItems.clear();
                if (s.length() == 0) {
                    aaItems.addAll(temp);
                } else {
                    for (String item : temp) {
                        if (item.toLowerCase().contains(txt)) {
                            aaItems.add(item);
                        }
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    public void btnAdd_Click(View v)
    {
        aaItems.add("Patrick");
    }

    public void PopulateArrayList()
    {
        arrMonth.add("January");
        arrMonth.add("February");
        arrMonth.add("March");
        arrMonth.add("April");
        arrMonth.add("May");
        arrMonth.add("June");
        arrMonth.add("July");
        arrMonth.add("August");
        arrMonth.add("September");
        arrMonth.add("October");
        arrMonth.add("November");
        arrMonth.add("December");
    }
}
resueman
  • 10,572
  • 10
  • 31
  • 45
  • Cool, glad it worked for you Patrick. I have two requests: If my answer helped you, could you kindly accept my answer? And secondly, the moderators will probably get on you about answering your own question as opposed to modifying your original question. Just an FYI. :) – Michael Krause May 19 '15 at 17:27
  • @MichaelKrause: It's perfectly fine to answer your own question. The main bit here is to see if it really did answer the question, is all. – Makoto May 19 '15 at 17:44