简体   繁体   中英

How to implement a SearchView in Android

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM