2

I am trying to implement a searchView in my app that enables the user to search a text across multiple lists. Something like below:

enter image description here

So far I have been able to implement a searchView that searches for text across a single list and it works great, but I can't think of a way to make it work across multiple lists with the section headers showing.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);

        item = menu.findItem(R.id.action_search);

        searchView = (SearchView) MenuItemCompat.getActionView(item);

        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setSuggestionsAdapter(mAdapter);

        searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
            @Override
            public boolean onSuggestionSelect(int position) {
                return false;
            }

            @Override
            public boolean onSuggestionClick(int position) {
//           some code here     
                return true;
            }
        });

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

            @Override
            public boolean onQueryTextChange(String query) {
                populateAdapter(query);
                return true;
            }
        });
//        return true;
        return true;
    }

private void populateAdapter(String query) {
        final MatrixCursor c = new MatrixCursor(new String[]{BaseColumns._ID, "cityName", "country"});
        for (City city: list) {
            if (city.getTitle().toLowerCase().startsWith(query.toLowerCase()))
                c.addRow(new Object[]{city.getID(), city.getTitle(), city.getNation()});
        }
        mAdapter.changeCursor(c);
    }

Any help would be appreciated, Thanks !!

Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71
varunkr
  • 5,364
  • 11
  • 50
  • 99
  • VENUES, CONCERTS are different lists of it's just a big list with sections in it? What should happen if after filtering you don't have anything for venues? – user Apr 21 '16 at 13:03
  • @Luksprog Thanks for the reply. "VENUES, CONCERTS are different lists of it's just a big list with sections in it". I can't say, this is not my app but I want a similar functionality where I will let the user search over two or three different fields, how it is going to be implemented is my doubt? " What should happen if after filtering you don't have anything for venues". In that case the section should not appear at all. So only movies and concerts would be shown. – varunkr Apr 21 '16 at 14:34
  • To the person who downvoted the question. Can you please tell why you did so? – varunkr Apr 21 '16 at 14:35
  • Your question is too broad and without a clear implementation the answer can only be vague: filter each of the lists of data and at the end after the filtering is done assemble the results as one. – user Apr 21 '16 at 16:41

1 Answers1

2

You can use this library to have different sections in your RecyclerView (movies, concerts, venues). Then you just need to filter the data of the sections in your onQueryTextChange method.

There is a full example here.

enter image description here

Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71
  • Thanks for the response, I solved this problem a while ago. But I would upvote your answer none the less. – varunkr May 11 '16 at 14:42
  • @varunkr would you mind adding your solution as an answer to your own question as well, so that others may use that solution too – PrashanD Jul 31 '18 at 04:03
  • 1
    the link got updated here is the updated link for example: https://github.com/luizgrp/SectionedRecyclerViewAdapter/blob/master/app/src/main/java/io/github/luizgrp/sectionedrecyclerviewadapter/demo/example7/Example7Fragment.java – Zulqarnain Mar 22 '21 at 10:25