过滤列表视图,我无法理解过滤规则

时间:2012-09-18 12:02:57

标签: android

我需要为ListView应用过滤器。我阅读了很多文档,但我无法理解过滤器的规则。过滤器有效,但我不知道它在做什么。

我使用这个类:

public class ArtistasActivity extends Activity {

private EditText filterText = null;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    View title = getWindow().findViewById(android.R.id.title);
    View titleBar = (View) title.getParent();
    titleBar.setBackgroundColor(Color.rgb(181, 9, 97));

    setContentView(R.layout.artistas_l);

    final ListView m_listview = (ListView) findViewById(R.id.listView1);

    ListAdaptor adaptor = new ListAdaptor(this, R.layout.artistas_l, loadArtistas());

    m_listview.setFastScrollEnabled(true);
    m_listview.setScrollingCacheEnabled(true);

    m_listview.setAdapter(adaptor);

    m_listview.setTextFilterEnabled(true);


    // Search

    filterText = (EditText) findViewById(R.id.search_box);
    filterText.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

        }

        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {

        }

        public void afterTextChanged(Editable text) {
            Log.d("search", ""+text);
            ListAdaptor adaptor = (ListAdaptor) m_listview.getAdapter();
            adaptor.getFilter().filter(text);
            adaptor.notifyDataSetChanged();
        }
    });     

ListAdaptor很典型:

    private class ListAdaptor extends ArrayAdapter<Artista> {
    private ArrayList<Artista> artistas;

    public ListAdaptor(Context context, int textViewResourceId, ArrayList<Artista> items) {
        super(context, textViewResourceId, items);
        this.artistas = items;
    }

    @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.artistas_list, null);
        }

        Object content = null;

        Artista o = artistas.get(position);

            TextView tt = (TextView) v.findViewById(R.id.ArtistTopText);

            tt.setText(o.getNombre());

            v.setClickable(true);
            v.setFocusable(true);
            v.setOnClickListener(myClickListener);


        return v;
    }
}

aplications有效,当在texteditor中写入时,列表会被过滤,但是当我放入R时,会出现Dani或Diego的名字......

我只覆盖了toString方法,只返回名称。

    @Override
public String toString() {
    return this.nombre;
}

求助! RGDS

1 个答案:

答案 0 :(得分:2)

没有必要维护aristas数组成员,因为您已经将它传递给super。在getView方法中,通过调用getItem(position)获取该项目。此更正将解决问题..