我有一个 BaseAdapter 并想使用过滤器。它充满了对象 (1)。我想使用“名称”字段进行过滤。当我尝试使用过滤器时,它给了我错误。检查代码:
对象接触
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;
}
}
基本适配器
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;
}
过滤器的调用
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);
}
});
编辑:
在搜索字段中输入内容后出现的错误:
关闭虚拟机
线程以未捕获的异常退出 (group=0x4001d7d0)
致命例外:主要
java.lang.NullPointerException
在 com.datalabrasil.dtldiscover.ContatoListAdapter.getCount(ContatoListAdapter.java:28)
在 android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:778)
在 android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)
在 android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
在 com.datalabrasil.dtldiscover.ContatoListAdapter$1.publishResults(ContatoListAdapter.java:85)
在 android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
在 android.os.Handler.dispatchMessage(Handler.java:99)
在 android.os.Looper.loop(Looper.java:123)
在 android.app.ActivityThread.main(ActivityThread.java:4627)
在 java.lang.reflect.Method.invokeNative(Native Method)
在 java.lang.reflect.Method.invoke(Method.java:521)
在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
在 dalvik.system.NativeStart.main(本机方法)