如何在listview中的列表视图项上添加搜索功能来自BaseAdapter

时间:2013-05-14 06:41:07

标签: android listview baseadapter

我必须在listview中的Edittext项目上添加搜索功能来自baseadapter 我正在使用此代码,但.getFilter()。filter(s.toString());不来了

我的代码是:

approvedfriendList.setAdapter(new ApprovedList());

            //*********Search Functionality in Approved Friend List************************
            serch_item.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    // TODO Auto-generated method stub
                //  approvedfriendList.getFilterTouchesWhenObscured().f
                }

                @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

                }
            });
    approvedfriendList is ArrayList 

我在这里没有得到如何使用.getFilter()。filter(s.toString()); with ApprovedList()(基本适配器类)

2 个答案:

答案 0 :(得分:0)

首先,您应该通过添加以下代码行启用列表视图进行过滤:

list.setTextFilterEnabled(true);

然后只需在文本视图中添加文本观察器:

text.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
              adapter.getFilter().filter( s.toString());
              list.setAdapter(adapter);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable prefix) { 
        }       

    });

答案 1 :(得分:0)

我在我的应用中实现了对联系人的搜索。 Followimg是代码的一部分:

searchContacts.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                temp.clear();
                for (int i = 0; i < emailIds.size(); i++) {
                    if (emailIds.get(i).toLowerCase().startsWith(s.toString())) {
                        temp.add(emailIds.get(i));
                    }
                }
                Collections.sort(temp);
                contacts.notifyDataSetChanged();

                for (int i = 0; i < temp.size(); i++) {
                    if (sqlHandler.isChecked(temp.get(i))) {
                        lvContacts.setItemChecked(i, true);
                    } else {
                        lvContacts.setItemChecked(i, false);
                    }
                }
            }

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

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });