時,我有一個自定義ArrayAdapter
像這樣的代碼:沒有結果使用自定義的ArrayAdapter和過濾
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();
}
}
}
我有一個SearchView
,我想過濾的名字行,但我有過濾後沒有結果。我相信,我有結果(從調試),但如果我把過濾器從我ListFragment
我看不到我的結果:
@Override
public boolean onQueryTextSubmit(String query) {
adapter.getFilter().filter(query);
return true;
}
更新我ArrayAdapter
從ListFragment
這樣的:
private void setListaUtenti(){
if(getListAdapter()==null){
// creo l'adapter
adapter=new UtenteAdapter(
getActivity(),
utenti);
setListAdapter(adapter);
} else{
adapter.notifyDataSetChanged();
}
}
爲什麼我沒有看到過濾操作的結果?
當您輸入一些過濾的文本時,您看不到過濾的值(或任何更改)? – Luksprog 2013-03-05 15:10:23
我可以進入onQueryTextSubmit但沒有元素過濾.. – fabio 2013-03-05 15:20:50