Android Tv Search Посмотрите, как использовать мой адаптер для предложений вместо провайдера - PullRequest
0 голосов
/ 25 февраля 2019

У меня есть приложение, объединенное для TV / Smarthone.MainActivity имеет SearchView с пользовательским адаптером sugggestion.Также у меня есть класс VideoProvider для глобального поиска в Android Tv.Глобальный поиск работает на Android Tv хорошо и SearchView показывают предложение в смартфоне.Но Android TV не показывает предложение.Если я удалю VideoProvider и Global Search соответственно, предложение на Android Tv будет работать.Я хочу использовать VideoProvider только для глобального поиска и пользовательский адаптер предложений только для SearchView.

MainActivity

 override fun onCreateOptionsMenu(menu: Menu): Boolean {

    menuInflater.inflate(R.menu.menu_activity_main, menu)
    val searchItem = menu.findItem(R.id.action_search)
    searchView = searchItem.actionView as SearchView
    if (MyPreferences.kidsMode)
        searchItem.isVisible = false
    searchView.maxWidth = Integer.MAX_VALUE
    searchAdapter = SearchSuggestionsAdapter(this, MyPreferences.theme == 0)
    searchView.suggestionsAdapter = searchAdapter


    searchView.setOnSuggestionListener(object : SearchView.OnSuggestionListener {
        override fun onSuggestionClick(position: Int): Boolean {
            searchView.clearFocus()
            val url = searchAdapter.getUrl(position)
            if (url.indexOf("actor") == 0 || url.indexOf("tag") == 0) {
                searchQuery(searchAdapter.getResult(position), url)
                return true
            }
            startActivity(PageFilmActivity.newIntent(this@MainActivity,
                    url,
                    null, true))
            //searchQuery(searchAdapter.getResult(position));
            return true
        }

        override fun onSuggestionSelect(position: Int): Boolean {
            return false
        }
    })
    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String): Boolean {
            searchQuery(query)
            return true
        }

        override fun onQueryTextChange(newText: String): Boolean {
            return false
        }
    })
    return true
}}

VideoProvider

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
class VideoProvider : ContentProvider() {

    companion object {
        private const val SEARCH_SUGGEST = 0
        private const val REFRESH_SHORTCUT = 1
        private const val TAG = "VideoProvider"
        private val URI_MATCHER = buildUriMatcher()

        fun buildUriMatcher(): UriMatcher {
            val matcher = UriMatcher(UriMatcher.NO_MATCH)
            // to get suggestions...
            Log.d(TAG, "suggest_uri_path_query: " + SearchManager.SUGGEST_URI_PATH_QUERY)
            matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST)
            matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST)
            return matcher
        }
    }

    override fun onCreate(): Boolean {
        Log.d(TAG, "onCreate")
        return true
    }

    override fun getType(uri: Uri?): String {
        return when (URI_MATCHER.match(uri)) {
            SEARCH_SUGGEST -> SearchManager.SUGGEST_MIME_TYPE
            REFRESH_SHORTCUT -> SearchManager.SHORTCUT_MIME_TYPE
            else -> throw IllegalArgumentException("Unknown URL $uri")
        }
    }


    override fun query(uri: Uri?, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor? {
        when (URI_MATCHER.match(uri)) {
            SEARCH_SUGGEST -> {
                val result = MatrixCursor(arrayOf(
                        SearchManager.SUGGEST_COLUMN_TEXT_1,
                        SearchManager.SUGGEST_COLUMN_RESULT_CARD_IMAGE,
                        SearchManager.SUGGEST_COLUMN_CONTENT_TYPE,
                        SearchManager.SUGGEST_COLUMN_PRODUCTION_YEAR,
                        SearchManager.SUGGEST_COLUMN_DURATION,
                        SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID))
                        .................................
                        .................................
                result.addRow(
                        arrayOf(name, imgUrl, MimeTypes.APPLICATION_MP4, 0, 0, url))
                return result
            }
            else -> throw IllegalArgumentException("Unknown Uri: $uri")
        }
    }



}

xml -levision / searchable

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
    android:hint="@string/search_hint"
    android:searchSuggestAuthority="com.example.app"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:searchSuggestIntentData="content://com.example.app/film"
    android:searchSuggestSelection=" ?"
    android:includeInGlobalSearch="true"
    >

</searchable>

xml / searchable

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
    android:hint="@string/search_hint" >
</searchable>
...