Reputation: 15062
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!
Upvotes: 3
Views: 11454
Reputation: 29
Try this way
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs.toString());
}
@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
}
});
Upvotes: 0
Reputation: 37729
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());
}
});
Upvotes: 5