在Android中处理MaterialSearchBar搜索结果

时间:2019-06-25 09:35:39

标签: android sqlite

我在我的应用程序中使用com.github.mancj:MaterialSearchBar:+依赖项来搜索SQLite数据库中的数据。它确实基于我在数据库中的数据加载建议,但是当用户单击建议之一并通过单击键盘中的搜索图标确认建议时,什么都不会显示,则不会调用适配器来显示结果。以下是我的搜索活动

 recyclerView = findViewById(R.id.hyme_list);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);

    materialSearchBar = findViewById(R.id.searchBar);

    database = new Database(this);

    materialSearchBar.setHint("Search here");
    materialSearchBar.setCardViewElevation(10);
    loadSuggestList();

    materialSearchBar.addTextChangeListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            List<String> sugest = new ArrayList<>();
            for (String search: suggestList) {
                if (search.toUpperCase().contains(materialSearchBar.getText().toUpperCase()))
                    sugest.add(search);

            }
                materialSearchBar.setLastSuggestions(sugest);
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

    materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
        @Override
        public void onSearchStateChanged(boolean enabled) {
            if (!enabled){

                searchAdapter = new SearchAdapter(getBaseContext(),database.getSongs());
                recyclerView.setAdapter(searchAdapter);
            }
        }

        @Override
        public void onSearchConfirmed(CharSequence text) {

            startSearch(text.toString());

        }

        @Override
        public void onButtonClicked(int buttonCode) {


        }
    });


    searchAdapter = new SearchAdapter(this,database.getSongs());
    recyclerView.setAdapter(searchAdapter);

}

private void startSearch(String text) {

    searchAdapter = new SearchAdapter(this,database.getSongByName(text));
    recyclerView.setAdapter(searchAdapter);

}

private void loadSuggestList() {
    suggestList = database.getTitle();
    materialSearchBar.setLastSuggestions(suggestList);

}

0 个答案:

没有答案