使用edittext搜索listview

时间:2014-05-01 19:02:01

标签: android android-listview

我有这个Listview:

@Override
        public void onComplete(List<Profile> friends) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mSpinner.setVisibility(View.GONE);
                    mSpinner.clearAnimation();
                }
                });

            // populate list
            List<String> values = new ArrayList<String>();
            for (Profile profile : friends) {
                //profile.getInstalled();
                values.add(profile.getName());
            }

            listView = (ListView) findViewById(R.id.list);
            listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> av, final View view, final int i, long i2) {

                  Animation pushLeftIn = AnimationUtils.loadAnimation(CallActivity.this, R.anim.jump_no_fade);
                  view.startAnimation(pushLeftIn);         

            }
            });

            ArrayAdapter<String> friendsListAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.list_items2, values);
            friendsListAdapter.sort(new Comparator<String>() {
                @Override
                public int compare(String lhs, String rhs) {
                    return lhs.compareTo(rhs);    
                }
            });

            mFriendsList.setAdapter(friendsListAdapter);

}
    };

我正在尝试使用edittext过滤器列表视图,这是我尝试过的:

mySearchView.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                    CallActivity.this.friendsListAdapter.getFilter().filter(cs);   
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                        int arg3) {
                    // TODO Auto-generated method stub

                }

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

我在代码中没有错误,但是当我尝试在edittext中搜索时,它根本不起作用,问题是什么或者我应该怎么做?

1 个答案:

答案 0 :(得分:0)

尝试在afterTextChanged(Editable arg0)中执行此操作。

您是否实施了getFilter().filter(cs)。你能展示实施吗?