在listview中实现搜索

时间:2014-12-09 15:10:05

标签: android listview

我想在播放列表中实现搜索。我在我的自定义数组适配器中实现了getFilter,但它修改了列表,所以如果我正在搜索一首歌并且它的位置是9并且在我的过滤器中它是唯一的结果,当我点击它时,将播放的歌曲是索引0而不是9。

我的代码:

list.setAdapter(adapter);

    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int start, int before, int count) {
            // When user changed the Text
            adapter.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
        }
    });

听单个listitem点击

lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // getting listitem index
            int songIndex = position;

            // Starting new intent
            Intent in = new Intent(getApplicationContext(),
                    MusicPlayerActivity.class);
            // Sending songIndex to PlayerActivity
            in.putExtra("songIndex", songIndex);
            setResult(SELECT_SONG, in);
            // Closing PlayListView
            finish();
        }
    });

我的代码在播放列表适配器中:

    @Override
    public Filter getFilter()
    {
        if (filter == null){
            filter  = new SongFilter();
        }
        return filter;
    }

private class SongFilter extends Filter
{
   @Override
   protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
           /* results.values = origonalList;
            results.count = origonalList.size();*/
            // We implement here the filter logic
            if (constraint == null || constraint.length() == 0) {
                synchronized (this)
                {
                    // No filter implemented we return all the list
                    results.values = MainAppConfig.playerService.getPlaylist();
                    results.count = MainAppConfig.playerService.getPlaylist().size();
                }
            }
            else {
                // We perform filtering operation
                ArrayList<Song> filter = new ArrayList<>();
                ArrayList<Song> items = new ArrayList<>();

                synchronized (this)
                {
                    items.addAll(songList);
                }

                for (Song song : items ) {
                    if (song.getFileName().toUpperCase().startsWith(constraint.toString().toUpperCase()))
                        filter.add(song);
                }

                results.values = filter;
                results.count = filter.size();
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            filteredList = (ArrayList)results.values;
            notifyDataSetChanged();
            clear();
            Log.d("Filter", "Starting to publish the results with " + songList.size() + " items");
            for (int i = 0;  i < filteredList.size(); i++){
                add(filteredList.get(i));
            }
            Log.d("Filter", "Finished publishing results");
            notifyDataSetInvalidated();
        }
}

1 个答案:

答案 0 :(得分:0)

列表视图此时并不知道适配器中项目的原始位置,但您可以使用以下内容自行查找:

Song song = parent.getItemAtPosition(position);
MainAppConfig.playerService.getPlaylist().indexOf(song);