首页 > 解决方案 > 保存搜索历史 Android Kotlin

问题描述

我正在使用 API 制作应用程序并完美地获取所有数据。我在回收站视图上也有搜索栏。现在我想保存用户在应用程序上搜索的搜索历史。我遵循了 stackoverflow 的解决方案,但我无法保存历史记录。

Android快速搜索中的搜索历史

我想知道我必须在哪里做searchable.xml?我也edittext用于在我的代码中执行搜索,所以我已经删除edittext并替换为searchable?

我在使用和设置content provider.

我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/txt_error_popular"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/connection_problem"
        android:visibility="gone"/>
    <EditText
        android:id="@+id/search_box"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:layout_marginEnd="10dp"
        android:layout_marginStart="10dp"
        android:hint="@string/search_for_something"
        android:layout_marginBottom="4dp"
        android:importantForAutofill="no" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_photos"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        android:scrollbars="vertical">

    </androidx.recyclerview.widget.RecyclerView>
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

main activity在哪里执行搜索:

search(DEFAULT_SEARCH)
    search_box?.setOnEditorActionListener(object : TextView.OnEditorActionListener{
        override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
            if (actionId == EditorInfo.IME_ACTION_SEARCH){
                val searchTerm = search_box.text.trim()
                if (searchTerm.isNotEmpty()){
                    rv_photos?.scrollToPosition(0)
                    photoAdapter.submitList(null)
                    search(searchTerm.toString())

                    currentFocus?.let {
                        val inputManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                        inputManager.hideSoftInputFromWindow(it.windowToken, HIDE_NOT_ALWAYS)
                    }
                }
                return true
            }
            return false
        }

    })
}
private fun search(searchTerm: String){
    viewModel.performSearch(searchTerm)
}

现在我只想保存最近的搜索?

标签: androidandroid-studiokotlin

解决方案


只需使用 roomDb 来存储您的搜索历史,每当用户点击搜索框时,就可以获取搜索列表

谷歌查看这个最新的代码实验室,它会引导你通过 roomDb https://developer.android.com/codelabs/android-room-with-a-view-kotlin#0


推荐阅读