I want to react to the user typing inside an EditText so I used the addTextChangedListener method.
When the user types a single character the code of onTextChanged is running and everything ok.
So if for example the user types "a" then onTextChanged will begin to run.
But if the user types another character, for example b , onTextChanged is not being called.
(the text in the EditText should be "ab" now)
The code:
et = (EditText)findViewById(R.id.edittextsearch);
et.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s){}
public void beforeTextChanged(CharSequence s, int start, int count,int after){}
public void onTextChanged(CharSequence s, int start, int before,int count)
{
int i = 0;
textlength=et.getText().length();
arr_sort.clear();
for(i=0;i<3;i++)
{
if(textlength<=your_array_contents[i].length())
{
if(et.getText().toString().equalsIgnoreCase((String) your_array_contents[i].subSequence(0, textlength)))
{
arr_sort.add(your_array_contents[i]);
}
}
}
lv.setAdapter(new ArrayAdapter<String>(GroupsActivity.this,
android.R.layout.simple_list_item_multiple_choice, arr_sort));
}
});
Help is appreciated!
From your code, What I could understand is, you want to filter the ListView.
Instead of doing filter by yourself you should use listView.setFilterText(String).
Like this way:
add your adapter for first and one time.
lv.setAdapter(new ArrayAdapter<String>(GroupsActivity.this,
android.R.layout.simple_list_item_multiple_choice, your_array_contents));
and then add TextWatcher:
txtFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length()==0){
lv.clearTextFilter();
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
lv.setTextFilterEnabled(true);
lv.setFilterText(s.toString());
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With