I try to implement the FirebaseListAdapter with a SearchView based on Android. Everything works well, but I can't filter my list of customers to the input text the user types in. Is there any way to filter the ListView-data to the string the user is typing? For example: There is a listview with many entries. The user is going to type : "Paul Miller" in the searchview, so the listview should just show every entry with the name "Paul Miller".
I tried to implement the Filterable Class to my own FirebaseListAdapter. The problem is I can't change the dataset (Query) like in an arraylist. So, notifydatasetchanged() is not working.
public class FirebaseCustomerAdapter extends FirebaseListAdapter<Customer> implements Filterable {
private CustomerFilter customerFilter;
private Query query;
public FirebaseCustomerAdapter(Activity activity, Query ref) {
super(activity, Customer.class, R.layout.fragment_customer_list_row, ref);
}
@Override
protected void populateView(View view, Customer customer, int position) {
TextView name = (TextView) view.findViewById(R.id.customer_list_row_name_textview);
TextView zip = (TextView) view.findViewById(R.id.customer_list_row_plz_city_textview);
if (customer.getName() != null) {
name.setText(customer.getName());
}
String zipCity = "";
if (customer.getZipCode() != null) {
zipCity = customer.getZipCode() + " ";
}
if (customer.getCity() != null) {
zipCity += customer.getCity();
}
zip.setText(zipCity);
}
@Override
public Filter getFilter() {
if (customerFilter == null) {
customerFilter = new CustomerFilter();
}
return customerFilter;
}
private class CustomerFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
Query query;
if (constraint != null && constraint.length() > 0) {
query = FirebaseHelper.getCustomerRef().orderByChild("name").equalTo(constraint.toString());
} else {
query = FirebaseHelper.getCustomerRef().orderByChild("name");
}
filterResults.values = query;
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
query = (Query) results.values;
}
}
}
Would be great to get your feedback.
Thanks
Michael