0

I have created a SearchActivity based on a RecyclerView that should search through an ArrayList<> that I have declared in my MainActivity

public class SearchActivity extends ListActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleIntent(getIntent());
    }

    public void onNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent(intent);
    }

    public void onListItemClick(ListView l, View v, int position, long id) {
        //DA IMPLEMENTARE: chiama accordo activity
        // do not care about this method, I'll implement it later
    }

    private void handleIntent(Intent intent) {
        if(Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            doSearch(query);
        }
    }

    private void doSearch(String queryStr) {
        // I think I should implement an adapter here
    }

}

I think I should implement something in the doSearch() method, but I do not know how to do it. I'd really appreciate if you could help me out

Daniele
  • 4,163
  • 7
  • 44
  • 95

1 Answers1

1

You should use a Filter and implement Filterable on your RecyclerView Adapter. Check this link out. or check this tutorials if you dont want to use Filter method

Community
  • 1
  • 1
Siddhesh Dighe
  • 2,894
  • 3
  • 16
  • 16
  • thank you for your answer! should I create a new class called SearchFilter and then implement it in the `doSearch()` method? – Daniele Jul 03 '16 at 14:15
  • No, use `onQueryTextChange` in your `doSearch ` method. check the implementation here http://stackoverflow.com/questions/30398247/how-to-filter-a-recyclerview-with-a-searchview/30429439#30429439 – Siddhesh Dighe Jul 03 '16 at 14:37
  • Heres a Video tutorial for better understanding of this concept https://www.youtube.com/watch?v=XtEtsMoOLog – Siddhesh Dighe Jul 03 '16 at 18:02
  • Thanks! I will try it out – Daniele Jul 03 '16 at 18:09
  • how do I use this [tutorial](https://www.numetriclabz.com/android-adding-search-functionality-to-recyclerview/) if I have a separate search activity and I still want to search my main activity list? – Daniele Jul 03 '16 at 18:56
  • Pass your arraylist from the mainactivity to the searchactivity with `Intent`, then provide the arraylist obtained from the mainactivity to the recyclerview in your search activity, and implement filterable on your recyclerview of searchactivity – Siddhesh Dighe Jul 03 '16 at 19:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116313/discussion-between-daniele-and-siddhesh-dighe). – Daniele Jul 03 '16 at 19:03