如何在android中基于edittext值更新listview

时间:2014-03-20 05:42:25

标签: android android-listview

我有edittext,下面是一个listview,如果需要基于edittext上的文本搜索方法,我需要更新列表视图。存储在arraylist中的listview值

enter image description here

1 个答案:

答案 0 :(得分:1)

这是我的应用程序中的搜索功能,希望你能从中得到它的想法..

et_artists_searchWord.addTextChangedListener(new TextWatcher() {

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

            List<AdapterDTOArtist> list = filter(s.toString(),
                    mAdapterDTOsBackup, true);
            mAdapterDTOs.clear(); // <--- clear the list before add
            mAdapterDTOs.addAll(list); // <--- here is the double add if
                                        // you// do not clear before
            mLazyAdatper.setDataSet(mAdapterDTOs);
        }

        @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

        }
    });

在oncreate搜索方法之外:

public static List<AdapterDTOArtist> filter(String string,
        Iterable<AdapterDTOArtist> iterable, boolean byName) {
    if (iterable == null)
        return new LinkedList<AdapterDTOArtist>();
    else {
        List<AdapterDTOArtist> collected = new LinkedList<AdapterDTOArtist>();
        Iterator<AdapterDTOArtist> iterator = iterable.iterator();
        if (iterator == null)
            return collected;
        while (iterator.hasNext()) {
            AdapterDTOArtist item = iterator.next();
            // if(item.getmTag_Name().startsWith(string))
            if (item.getmTag_Name().toLowerCase().startsWith(string)){
                // if
                // (item.getmTag_Name().toLowerCase(Locale.getDefault()).contains(string))
                collected.add(item);
            }
            else if(item.getmTag_Name().toUpperCase().startsWith(string)){
                collected.add(item);
            }
        }
        return collected;
    }
}