BaseAdapterがあり、フィルターを使用したいと思います。オブジェクト(1)でいっぱいです。「nome」フィールドを使用してフィルタリングしたい。フィルタを使用しようとするとエラーが発生します。コードを確認してください:
オブジェクトコンタト
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;
}
フィルタの呼び出し
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);
}
});
編集:
検索フィールドに何かを入力した後に発生したエラー:
VMをシャットダウンする
キャッチされない例外で終了するスレッド(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(ネイティブメソッド)で
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(ネイティブメソッド)で