0

我有一个 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(本机方法)

4

1 回答 1

1
protected List<Contato> getFilteredResults(CharSequence constraint) {
        // TODO Auto-generated method stub
        return null;
    }

我认为你必须填写这个方法的主体,返回正确的结果:)

编辑:试试这个:

protected List<Contato> getFilteredResults(CharSequence constraint) {
    ArrayList<Contacto> filteredList = new ArrayList<Contato>(); 
    for (Contato c:lista) { 
        if (c.nome.trim().toLowerCase().startsWith(constraint.toString().trim().toLowerCase())) { // you can also use contains instead of startsWith...whatever you need 
            filteredList.add(c); 
        } 
    } 
    return filteredList;
    }

编辑#2:在for循环中使用类似或类似的东西可能会更好getAllContato(),而不是lista,因为过滤后,过滤后的结果会重写变量lista,这意味着每次过滤时,您将拥有越来越少的项目您的列表...在这种情况下,您正在过滤过滤的项目等等...我希望您能明白我想说的话。:)

于 2012-08-27T22:18:56.990 回答