1

我需要为 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;
    }
}

应用程序有效,当在文本编辑器中写入时,列表被过滤,但是当我输入 R 时,出现带有 Dani 或 Diego 的名称......

我重写 toString 方法只返回名称。

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

谢谢帮助!Rgds

4

1 回答 1

2

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

于 2012-09-18T13:31:28.673 回答