0

I am making app using API and fetched all the data perfectly. I also have search bar on recycler view. Now I want to save the search history which user searches on the app. I have followed the solutions from stackoverflow but I am not able to save the history.

Search history in Android quick search

I want to know that where I have to make searchable.xml ? Also I am using edittext for performing search in my code so I have remove edittext and replace with searchable?

I little confused in using and setting up the content provider.

My xml file:

<?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>

My main activity where I am performing search:

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)
}

Now I just want to save the recent search?

Harry
  • 154
  • 4
  • 15
  • 1
    Hi, Just use roomDb to store your search history and whenever the user clicks on the search box fetch the search list – Nitin Prakash Apr 23 '21 at 14:04
  • @NitinPrakash: Here I don't want to use any database for storing the history. I just want to store the strings which user search in the search box from the app. For example, when we tap on google search engine then we should see our previous search - something similar. – Harry Apr 23 '21 at 15:37
  • 1
    So how you will get the previous search strings if you don't use local DB? – Nitin Prakash Apr 24 '21 at 04:44
  • @NitinPrakash: Thank you for giving me the direction. I must have to use the DB. After giving the comment to you, I have searched a lot and spend the time and I have solved this problem by using localDB. – Harry Apr 24 '21 at 13:41

1 Answers1

0

Just use roomDb to store your search history and whenever the user clicks on the search box fetch the search list

Check this latest codelab by google which guides you through roomDb https://developer.android.com/codelabs/android-room-with-a-view-kotlin#0

Nitin Prakash
  • 927
  • 9
  • 16