0

I have a custom ArrayAdapter like in this code:

public class UtenteAdapter extends ArrayAdapter<Utente> implements Filterable {
private Context context;
private ArrayList<Utente> utenti;
private ArrayList<Utente> utentiFiltrati;
private FiltroPersonalizzato filtro;

public UtenteAdapter(Context context, ArrayList<Utente> utenti) {
    super(context, R.layout.riga_utente, utenti);
    this.context=context;
    this.utenti=utenti;
    utentiFiltrati = new ArrayList<Utente>();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //omitted..
    return riga;
}
@Override
public void notifyDataSetChanged(){
    super.notifyDataSetChanged();
}

@Override
public Filter getFilter() {
    if (filtro == null) {
        filtro = new FiltroPersonalizzato();
    }
    return filtro;
}
private class FiltroPersonalizzato extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence prefix) {
        FilterResults risultato = new FilterResults();
        ArrayList<Utente> i = new ArrayList<Utente>();
          if (prefix!= null && prefix.toString().length() > 0) {
              // use the initial values !!! 
              for (int index = 0; index < utenti.size(); index++) {
                  Utente si = utenti.get(index);
                  final int length = prefix.length();
                  // if you compare the Strings like you did it will never work as you compare the full item string(you'll have a match only when you write the EXACT word)
                  // keep in mind that you take in consideration capital letters!
                  if(si.getNome().toLowerCase().substring(0, length).compareTo(prefix.toString().toLowerCase()) == 0){
                    i.add(si);  
                  }
              }
              risultato.values = i;
              risultato.count = i.size();                   
          }
          else{
              // revert to the old values 
              synchronized (utentiFiltrati){
                  risultato.values = utenti;
                  risultato.count = utenti.size();
              }
          }
          return risultato;
    }
    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        utentiFiltrati = (ArrayList<Utente>)results.values;
        notifyDataSetChanged();
    }
}
}

I have a SearchView and I want to filter the rows by name but I have no results after filtering. I am sure that I have results(from debugging) but if I call filter from my ListFragment I can't see my result:

        @Override
        public boolean onQueryTextSubmit(String query) {
            adapter.getFilter().filter(query); 
            return true;
        }       

I update my ArrayAdapter from the ListFragment like this:

private void setListaUtenti(){

    if(getListAdapter()==null){
        // creo l'adapter
        adapter=new UtenteAdapter(
                getActivity(),
                utenti);
        setListAdapter(adapter);  

    } else{
        adapter.notifyDataSetChanged();         
    }                       
}

Why don't I see the results of the filtering operation?

2
  • You don't see the filtered values(or any change at all) when you enter some filtered text? Commented Mar 5, 2013 at 15:10
  • i can go into onQueryTextSubmit but no element filtered.. Commented Mar 5, 2013 at 15:20

1 Answer 1

1

i have a searchview and i want filter row by name but i have no result.

It's normal that you don't get a result as in the publishResults() callback of the Filter you assign the results to the utentiFiltrati list and call notifyDataSetChanged(). This will do nothing as your adapter is based on the utenti list, the one you pass to the super class constructor. Make the following changes:

public UtenteAdapter(Context context, ArrayList<Utente> utentiValues) {
    super(context, R.layout.riga_utente, utentiValues);
    this.context=context;
    this.utentiFiltrati = utentiValues;
    utenti = new ArrayList<Utente>(utentiValues);
}

// ...
FilterResults risultato = new FilterResults();
ArrayList<Utente> i;
if (prefix == null || prefix.toString().length() == 0) {
     // the contract of a Filter says that you must return all values if the
     // challenge string is null or 0 length
     i = new ArrayList<Utente>(utenti);
} else {
     i = new ArrayList<Utente>();
     // use the list that contains the full set of data
     for (int index = 0; index < utenti.size(); index++) {
           Utente si = utenti.get(index);
           final int length = prefix.length();
           if(si.getNome().toLowerCase().substring(0, length).compareTo(prefix.toString().toLowerCase()) == 0){
                i.add(si);  
           }
     }
  //...
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
        FilterResults results) {
    utentiFiltrati = (ArrayList<Utente>)results.values;
    notifyDataSetChanged();
}
Sign up to request clarification or add additional context in comments.

7 Comments

no change i tried: adapter.getFilter().filter(query); setListaUtenti(); but i have no result yet..
@fabio How about if you do clear(); utentiFiltrati = (ArrayList<Utente>)results.values; for (Utente item : utentiFiltrati) { add(item);} in the publishResults() method?
yes now i have a result! (but it is wrong..) how i can come back before search?
@fabio What exactly does but it is wrong mean? Come back before search - Are you trying to show the unfiltered list(in which case you could just filter the list again with a null string)?
i resolved my problem (on getview i must use array filtered) thanks Luksprog!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.