通过它的ArrayAdapter过滤多行列表视图

时间:2012-05-22 09:58:53

标签: android listview filter android-arrayadapter

我正在创建一个带有ArrayAdapter的ListView,如下所示:

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

final EntryAdapter adapter = new EntryAdapter(this, items);
        lv.setAdapter(adapter);

和适配器看起来像:

this.items = items;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);



    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder;

        final Item i = items.get(position);
        if (i != null) {
            if(i.isSection()){
                SectionItem si = (SectionItem)i;
                v = vi.inflate(R.layout.list_item_section, null);


                v.setOnClickListener(null);
                v.setOnLongClickListener(null);
                v.setLongClickable(false);

                final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
                sectionView.setText(si.getTitle());
            }else{
                EntryItem ei = (EntryItem)i;
                v = vi.inflate(R.layout.carlistdetails, null);
                holder = new ViewHolder();
                final TextView name = (TextView)v.findViewById(R.id.name);
                final TextView location = (TextView)v.findViewById(R.id.location);
                final TextView speed = (TextView)v.findViewById(R.id.speed);
                final ImageView image = (ImageView) v.findViewById(R.id.imageView1);

                if (name != null) 
                    name.setText(ei.name);
                if(location != null)
                    location.setText(ei.location);
                if(speedandcontact != null)
                    speed.setText(ei.speed);
                    image.setImageResource(R.drawable.car);


            }
        }
        return v;
    }

我有一个listview,每个项目包含3个TextView和1个ImageView。 现在我试图通过

过滤适配器
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) {
        adapter.getFilter().filter(s);
    }

};


EditText filterText = (EditText) findViewById(R.id.sb);
filterText.addTextChangedListener(filterTextWatcher);

这有点工作,但给出了奇怪的结果(大多数时候所有项目都从列表中消失)。我想只通过适配器/项目的名称字段进行过滤。

关于如何做到这一点的任何想法?

2 个答案:

答案 0 :(得分:1)

this issue as like as from that: http://stackoverflow.com/a/13156885/1752867
you can try use your custom adapter and filter.

代码:

a: for (int i = 0; i < allObjectItemsArray.size(); i++) {
                T m = allObjectItemsArray.get(i);
                        // cast m to your class
                String fistnameAndLastName = ((your object class) m).getFirstName() + ((your object class) m).getLastName();

                if (fistnameAndLastName.toLowerCase().contains(constraint)) {
                    filteredItems.add(m);
                    continue a;
                }

            }

you can modify this line String fistnameAndLastName = ((your object class) m).getFirstName() + ((your object class) m).getLastName();

to 

like : String name = ((your object class) m).getName(); 

答案 1 :(得分:0)

此链接显示了如何实施自定义过滤器:

http://www.mokasocial.com/2010/07/arrayadapte-filtering-and-you/

如果列表视图是一个对象数组,则需要执行此操作。过滤器需要知道要过滤的视图部分。