RecyclerView + searchView

时间:2016-02-20 16:34:11

标签: android search android-recyclerview

我的应用中的搜索选项出现问题。我使用的是recyclerView,一切正常......除了searchView之外的一切。在搜索字段中输入查询不会在ListView中进行任何更改。也许你可以帮助我?我的适配器中有方法替换,如下所示:

 public void replace(List<Concert> concerts) {
        if (concerts != null) {
            concertList = concerts;
            notifyDataSetChanged();
        }

    }

然后我的片段类看起来像这样:

public class SearchFragment extends Fragment {


SearchView mSearchView;
RecyclerView mSearchRecyclerView;
Activity mActivity;
SearchAsyncTask mSearchAsyncTask;
Concert concert;
List<Concert> searchedList = new ArrayList<>();
ConcertAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_search_layout, container, false);
    mSearchView = (SearchView) v.findViewById(R.id.search_view);
    mSearchView.setQueryHint("Kogo chcesz posłuchać?");
    mSearchRecyclerView = (RecyclerView) v.findViewById(R.id.search_concerts_rv);
    adapter = new ConcertAdapter(getActivity().getApplicationContext(), new ArrayList<Concert>(), (MainActivity) mActivity);
    mSearchRecyclerView.setAdapter(adapter);
    mSearchRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
    mSearchAsyncTask = new SearchAsyncTask(SearchFragment.this);
    futureConcerts();

    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            Log.d("Search", "SearchOnQueryTextSubmit: " + s);
            search(s);
            return true;

        }
    });

    return v;
}
public void futureConcerts() {
    mSearchAsyncTask.execute();
    getActivity().getWindow().getDecorView().getRootView().setClickable(false);
}
public void notifyAboutListCreation(List<Concert> res) {
    adapter.replace(res);

}
private boolean search(String search) {
    List<Concert> subList = new ArrayList<>();
    for (Concert concert : searchedList) {
        if (concert.getTitle().toLowerCase().contains(search.toLowerCase())) {
            subList.add(concert);
            Log.d("added", concert.getTitle());
        }

        adapter.replace(subList);
    }

    return true;
}

}

你可以写任何你想要的东西,但listView保持不变:/任何想法它为什么不起作用?

1 个答案:

答案 0 :(得分:0)

因为searchingList为空。您正在寻找空列表中的音乐会。