I am using a listview
to display a list of data through a custom adaptor.
And I have a search textbox in which I have to filter text in the List view.
Code:
adapter = new ListCustomersAdapter(context, Customers);
lsCustomers = (ListView) findViewById(R.id.list_customers);
lsCustomers.setTextFilterEnabled(true);
edtSearch=(EditText)findViewById(R.id.edtSearch);
edtSearch.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Log.d("","adapter: "+adapter);
adapter.getFilter().filter(cs);
lsCustomers.setAdapter(adapter);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3)
{
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
and this my adapter class,
public class ListCustomersAdapter extends ArrayAdapter implements Filterable {
public Context ctx;
public String[] customers;
LayoutInflater mInflater;
private boolean notifyChanged = true;
public ListCustomersAdapter(Context c,
String[] Customers ){
super(c, 0);
mInflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.ctx=c;
this.customers=Customers;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return customers.length;
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
notifyChanged = true;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View localView;
if (convertView == null) {
localView = mInflater
.inflate(R.layout.list_view_layout, null);
} else {
localView = convertView;
}
TextView txtCustomername=(TextView)localView.findViewById(R.id.txtsimple_list);
txtCustomername.setText(customers[position]);
return txtCustomername;
}
}
But still I am not able to get the filtered text. Showing the Listview
with original list items even after I entered the search Text in the textbox.