FirebaseListAdapter with filtered data

2,026 views
Skip to first unread message

Michael Huber

unread,
Jun 17, 2016, 10:53:34 AM6/17/16
to Firebase Google Group
Hi,

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

Frank van Puffelen

unread,
Jun 18, 2016, 7:00:36 PM6/18/16
to Firebase Google Group
The FirebaseUI adapters for Android don't yet have support for client-side filtering. The request to add this is tracked in [this Github issue](https://github.com/firebase/FirebaseUI-Android/issues/15).

Adding support for client-side filtering with a Filterable seems like a beautiful idea. I had never seen that interface, so thanks for pointing me to it. I've added a note to the issue about the possibility of using  Filterable. Keep in mind though that we keep the functionality of FirebaseUI in sync between Android and iOS. So knowing a beautiful approach for Android does not necessarily mean we'll be able to add it soon.

      puf
Reply all
Reply to author
Forward
0 new messages