listView filter过滤

先看例子演示:

第一个是arrayAdapter,第二个是baseAdapter

listView filter过滤_第1张图片


部分java代码:

package mm.shandong.com.testbaseadapterfilter;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;


import java.util.ArrayList;
import java.util.List;

import mm.shandong.com.testbaseadapterfilter.adapter.BaseAdapterFilter;
import mm.shandong.com.testbaseadapterfilter.entity.Person;

public class TestBaseAdapterFilterActivity extends AppCompatActivity {
    ListView listView1;
    ListView listView2;
    EditText editText;
    List persons;
    String[] person_names;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_base_adapter_filter);
        listView1 = (ListView) findViewById(R.id.listView1);
        listView2 = (ListView) findViewById(R.id.listView2);
        //查询编辑框
        editText = (EditText) findViewById(R.id.editText);
        initData();
        //listView1放置arrayAdapter,arrayAapter自带过滤字符串的filter
        ArrayAdapter arrayAdapter1 =
                new ArrayAdapter(this,
                        android.R.layout.simple_list_item_1,
                        person_names);
        final BaseAdapterFilter arrayAdapter2 =
                new BaseAdapterFilter(this, persons);
        listView1.setAdapter(arrayAdapter1);
        listView1.setTextFilterEnabled(true);//关键代码
        //listView2放置baseAdapter,baseAdapter没有自带的过滤功能,
        // 需要在adapter中实现过滤功能
        listView2.setAdapter(arrayAdapter2);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence,
                                          int i, int i1, int i2) {

            }
           ///注册编辑框文本发生变化时的监听方法
            @Override
            public void onTextChanged(CharSequence charSequence,
                                      int i, int i1, int i2) {
                String key = charSequence.toString();
                if (TextUtils.isEmpty(key)) {
                    listView1.clearTextFilter();
                } else {
                    listView1.setFilterText(key);
                }
                arrayAdapter2.getFilter().filter(charSequence);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }
    //初始化数据源
    public void initData() {
        person_names = new String[]{"张三",
                "曹操", "王二", "张飞", "赵云", "刘备"};
        persons = new ArrayList();
        Person person = new Person("张三", 20, "1001");
        persons.add(person);
        person = new Person("李四", 21, "1002");
        persons.add(person);
        person = new Person("王二", 22, "1003");
        persons.add(person);

        person = new Person("张飞", 23, "1004");
        persons.add(person);
        person = new Person("赵云", 24, "1005");
        persons.add(person);
        person = new Person("马岱", 25, "1006");
        persons.add(person);
    }

}
Demo下载


最后,以上例子都来源与安卓无忧,请去应用宝或者豌豆荚下载:http://android.myapp.com/myapp/detail.htm?apkName=com.shandong.mm.androidstudy,源码例子文档一网打尽。

你可能感兴趣的:(listView filter过滤)