Android 之EditText自动检索Listview中的数据

导读:本篇文章讲解 Android 之EditText自动检索Listview中的数据,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1.直接上代码吧。

public class MainActivity extends Activity {
    private ListView mlistview;
    private EditText medittext;
    List<PersionInfo>lists=new ArrayList<PersionInfo>();
    private PersionInfoAdapter mAdapter;
    //造数据


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mlistview=(ListView) findViewById(R.id.mlistview);
        medittext=(EditText) findViewById(R.id.medittext);
        //造数据
        lists.add(new PersionInfo("易皇星", "测试数据"));
        lists.add(new PersionInfo("易皇星", "测试数据"));
        lists.add(new PersionInfo("测试", "测试数据"));
        lists.add(new PersionInfo("测试", "测试数据"));

        //
        mAdapter=new PersionInfoAdapter(this, lists);
        mlistview.setAdapter(mAdapter);

        //
        medittext.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                filterData(s.toString());

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });
    }




    class PersionInfoAdapter extends BaseAdapter{
        private  List<PersionInfo>lists;
        private Context context;
        private LayoutInflater imInflater;


        public PersionInfoAdapter(Context context,List<PersionInfo>lists){
            this.context=context;
            this.lists=lists;
            imInflater = LayoutInflater.from(context);
        }
        //刷新adapter方法
        public void updateListView(List<PersionInfo> newlists){
            lists=newlists;  //重新赋值
            notifyDataSetChanged();  //刷新
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return lists.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return lists.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //这里就不进行优化了
            View inflate = imInflater.inflate(R.layout.listview_item, null);
            TextView tv_name=(TextView) inflate.findViewById(R.id.tv_name);
            TextView tv_hobby=(TextView) inflate.findViewById(R.id.tv_hobby);

            PersionInfo persionInfo = lists.get(position);
            tv_name.setText(persionInfo.getName());
            tv_hobby.setText(persionInfo.getHobby());

            return inflate;
        }

    }

    /**
     * 根据输入框中的值来过滤数据并更新ListView
     * @param filterStr
     */
    private void filterData(String mesg){
        //创建新的集合
        List<PersionInfo> newlists=new ArrayList<PersionInfo>();
        if(TextUtils.isEmpty(mesg)){
            newlists=lists;
        }else {
            newlists.clear();
            //根据lists集合中的对象字段名过滤
            for(PersionInfo sortModel : lists){
                String name = sortModel.getName();
                if(name.indexOf(mesg.toString()) != -1 ){
                //重新添加到新的集合中
                    newlists.add(sortModel);
                }
            }
        }
        // 不管怎么样都要刷新
        mAdapter.updateListView(newlists);
    }

}

2.效果图。
这里写图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/12879.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!