具有自定义toString()的对象的TextWatcher过滤器

时间:2012-06-28 00:42:38

标签: android textwatcher

我正在尝试从ListActivity过滤自定义ListView。问题是,当过滤器实际工作时,ListView无法正确显示。

例如,如果我有3个项目,分别以a,b,c,da,db开头,输入e将显示ListView为空。但是,当我输入b时,它会显示一个,当我输入c时,它会显示一个。当我输入d时,它会显示a,b。我不完全确定我做错了什么或者我错过了什么问题。看来这个位置没有被传递给过滤器,但是我该怎么做呢?

以下是我的ArrayAdapter:

private class SiteAdapter extends ArrayAdapter<Site> {
    private ArrayList<Site> sites;

    public SiteAdapter(Context context, int textViewResourceId, ArrayList<Site> sites) {
        super(context, textViewResourceId, sites);
        this.sites = sites;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        Site s = this.sites.get(position);
        if (s != null) {
            TextView tt = (TextView) v.findViewById(R.id.toptext);
            TextView bt = (TextView) v.findViewById(R.id.bottomtext);
            TextView rt = (TextView) v.findViewById(R.id.statustext);
            if (tt != null) {
                tt.setText("sup2");
            }
            if (bt != null) {
                bt.setText("sup");
            }
            if (rt != null) {
                rt.setText("sup");
            }
        }
        v.setOnClickListener(new OnItemClickListener(position));
        return v;
    }

    private class OnItemClickListener implements OnClickListener{           
        private int mPosition;
        OnItemClickListener(int position){
                mPosition = position;
        }
        @Override
        public void onClick(View arg0) {
            //Log.v("MainActivity", sites.get(mPosition).getURL());
            Intent intent = new Intent(getBaseContext(), Wee.class);
            startActivityForResult(intent,1);
        }               
    }
}


private TextWatcher filterTextWatcher = 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) {
        site_adapter.getFilter().filter(s);
    }
};

@Override
protected void onDestroy() {
    super.onDestroy();
    filterText.removeTextChangedListener(filterTextWatcher);
}

我的对象类有:

@Override
public String toString() {
    return new StringBuilder()
    .append(this.SEARCHABLE).toString();
}

我把它归结为正确过滤,但现在过滤器在删除或清空文本框时没有将Listview返回到其原始状态。我从Custom Filtering ArrayAdapter in ListView获得了示例:

private class SiteFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            String prefix = constraint.toString().toLowerCase();

            if (prefix == null || prefix.length() == 0) {
                ArrayList<Site> list = new ArrayList<Site>(sites);
                results.values = list;
                results.count = list.size();
            } else {
                final ArrayList<Site> list = new ArrayList<Site>(sites);
                final ArrayList<Site> nlist = new ArrayList<Site>();
                int count = list.size();

                for (int i = 0; i < count; i++) {
                    final Site pkmn = list.get(i);
                    final String value = pkmn.getFILTERABLETEXT().toLowerCase();

                    if (value.startsWith(prefix)) {
                        nlist.add(pkmn);
                    }
                }
                results.values = nlist;
                results.count = nlist.size();
            }
            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filtered_sites = (ArrayList<Site>) results.values;

            clear();
            int count = filtered_sites.size();
            for (int i = 0; i < count; i++) {
                Site pkmn = (Site) filtered_sites.get(i);
                add(pkmn);
            }
        }
    }

钱德拉的建议:

@SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filtered_sites = (ArrayList<Site>) results.values;
            site_adapter.notifyDataSetChanged();
            clear();
            int count = filtered_sites.size();
            for (int i = 0; i < count; i++) {
                Site pkmn = (Site) filtered_sites.get(i);
                add(pkmn);
                Log.d("OMGOMGOMG",pkmn.getURL());
            }
            site_adapter.notifyDataSetInvalidated();
        }

1 个答案:

答案 0 :(得分:0)

我认为它没有通知数据的变化。在方法notifyDataSetChanged

中添加onTextChanged
public void onTextChanged(CharSequence s, int start, int before, int count) {
    site_adapter.getFilter().filter(s);
    site_adapter.notifyDataSetChanged(); 
}