I have a BaseAdapter and want use filter it. Its filled with object (1). I want filter using the field “nome”. Its give me error when i try use the filter. check the code:
Object Contato
public class Contato {
public static final String CONTATO_NOME = "contato_nome";
public static final String CONTATO_ORG = "contato_org";
public static final String CONTATO_ID = "contato_id";
public static String[] colunas = {"_id", Contato.CONTATO_NOME, Contato.CONTATO_ORG, Contato.CONTATO_ID};
public long _id;
public String nome;
public String org;
public String id;
public Contato () {
}
public Contato (String nome, String org, String id) {
this.nome = nome;
this.org = org;
this.id = id;
}
public Contato (long _id, String nome, String org, String id) {
this.id = id;
this.nome = nome;
this.org = org;
this.id = id;
}
public static String[] getColunas() {
return colunas;
}
public static void setColunas(String[] colunas) {
Contato.colunas = colunas;
}
public long get_Id() {
return _id;
}
public void set_Id(long _id) {
this._id = _id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getOrg() {
return org;
}
public void setOrg(String org) {
this.org = org;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
baseadapter
public class ContatoListAdapter extends BaseAdapter implements Filterable {
private Context context;
private List<Contato> lista;
private TextView nome;
private TextView org;
private LayoutInflater inflater;
public ContatoListAdapter(Context contexto, List<Contato> lista) {
this.context = contexto;
this.lista = lista;
}
public int getCount() {
return lista.size();
}
public Object getItem(int position) {
return lista.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Recupera a atividade da posição atual
Contato c = lista.get(position);
if (position == 0) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.contato_linha_lista, null);
// Atualiza o valor do TextView
nome = (TextView) view.findViewById(R.id.txtContatoListaNome);
nome.setText(c.nome);
org = (TextView) view.findViewById(R.id.txtContatoListaOrganizacao);
org.setText(c.org);
return view;
} else {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.contato_linha_lista, null);
// Atualiza o valor do TextView
nome = (TextView) view.findViewById(R.id.txtContatoListaNome);
nome.setText(c.nome);
org = (TextView) view.findViewById(R.id.txtContatoListaOrganizacao);
org.setText(c.org);
return view;
}
}
public Filter getFilter() {
return new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
lista = (List<Contato>) results.values;
ContatoListAdapter.this.notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Contato> filteredResults = getFilteredResults(constraint);
FilterResults results = new FilterResults();
results.values = filteredResults;
return results;
}
};
}
protected List<Contato> getFilteredResults(CharSequence constraint) {
// TODO Auto-generated method stub
return null;
}
filter’s call
Util.etxtTelaContatosListaBuscar
.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (Util.adapter != null)
Util.adapter.getFilter().filter(s);
}
});
EDIT:
errors I got after type something in search field:
Shutting down VM
thread exiting with uncaught exception (group=0x4001d7d0)
FATAL EXCEPTION: main
java.lang.NullPointerException
at
com.datalabrasil.dtldiscover.ContatoListAdapter.getCount(ContatoListAdapter.java:28)at
android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:778)at
android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)at
android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)at
com.datalabrasil.dtldiscover.ContatoListAdapter$1.publishResults(ContatoListAdapter.java:85)at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
at dalvik.system.NativeStart.main(Native Method)
i think you have to fill this method’s body, returning proper result 🙂
edit: try this:
edit #2: in the for cycle would be maybe better to use something like
getAllContato()or something like that, instead of lista, because after filtering, filtered results rewrite variable lista and that means that with every filtering, you will have less and less items in your list…in that case you are filtering filtered items and so on…i hope you get what i am trying to say. 🙂