过滤列表视图,我不明白过滤规则我不、视图、规则、列表

2023-09-07 08:54:00 作者:煽情戏子゛

我需要应用过滤器的一个ListView。我看了很多资料,但我无法理解过滤器的规则。该滤波器的作品,但我不知道什么是它做的。

i need apply a filter for a ListView. I read a lot of documentation but i can't understand the rule of the filter. The filter works but i don't know what are it doing.

我使用这个类:

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是典型的:

And ListAdaptor is typical:

    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工作,当写在文本编辑过滤列表,但是当我把R,出现与丹妮或迭戈的名字...

The aplications works, and when write in the texteditor the list is filtered but when i put a R, appear names with Dani or Diego...

我重写toString方法只返回德的名字。

I override the toString method for only return de name.

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

THX的帮助! RGDS

Thx for help !! Rgds

推荐答案

有没有必要保持 aristas 数组成员,因为你已经把它传递给超。在 getView 方法,通过调用获得该项目的getItem(位置) ..此更正将解决这个问题..

There is no need to maintain the aristas array member, as you are already passing it to the super. In the getView method, get the item by calling getItem(position).. This correction will fix the problem..