Gaurav-Chaudhary1 / Quick-Notes-App

"Developed a robust notes-taking app for Android using Kotlin, SQLite, and XML. Users can effortlessly create, update, and delete notes, with added features for search, completion ticking, and pinning. Implemented a Material Design interface for a visually appealing and intuitive user experience."
1 stars 0 forks source link

Seach functionality #1

Open rhubka opened 1 month ago

rhubka commented 1 month ago

I can't seem to get the search functionality to work. I entered and saved about 10 records and that works great. I'm using the latest android studio with a Google Pixel 5 mobile. Any ideas? Thanks

Gaurav-Chaudhary1 commented 1 month ago

Sorry to hear that you are facing a issue with the search functionality in my project. I am glad that you noticed it and reached to me.

Here's how you can implement the search functionality in Kotlin for your notes app, so that the search results appear at the top of the list.

1. Add a SearchView in your XML layout:

<SearchView
    android:id="@+id/search_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:queryHint="Search notes" />

2. Handle Search in Activity or Fragment:

In your Kotlin Activity or Fragment, you need to set up the SearchView to listen for text changes and then update the list based on the query.

val searchView = findViewById<SearchView>(R.id.search_view)

// Set up the listener for search view
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
    override fun onQueryTextSubmit(query: String?): Boolean {
        // No need to handle submit if you're filtering in real-time
        return false
    }

    override fun onQueryTextChange(newText: String?): Boolean {
        // Filter the notes as the user types
        newText?.let { notesAdapter.filter(it) }
        return true
    }
})

3. Filter Logic in Adapter:

In your adapter, you need a method that will filter the list of notes and update the displayed items in your RecyclerView.

class NotesAdapter(private var notesList: MutableList<Note>) :
RecyclerView.Adapter<NotesAdapter.NoteViewHolder>() {

    private var filteredNotesList: MutableList<Note> =
notesList.toMutableList()

    // Filter method
    fun filter(query: String) {
        val filteredList = notesList.filter {
            it.title.contains(query, ignoreCase = true) ||
it.content.contains(query, ignoreCase = true)
        }.toMutableList()

        // Sort the filtered list if needed (optional)
        filteredList.sortBy { it.title }

        // Update the filtered list and notify the adapter
        updateList(filteredList)
    }

    // Update the list and notify the adapter
    private fun updateList(newList: MutableList<Note>) {
        filteredNotesList.clear()
        filteredNotesList.addAll(newList)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
NoteViewHolder {
        val itemView =
LayoutInflater.from(parent.context).inflate(R.layout.item_note, parent,
false)
        return NoteViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: NoteViewHolder, position: Int) {
        val note = filteredNotesList[position]
        holder.bind(note)
    }

    override fun getItemCount(): Int {
        return filteredNotesList.size
    }

    // ViewHolder class
    class NoteViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
        fun bind(note: Note) {
            // Bind the data to your view elements
        }
    }
}

4. RecyclerView Update:

After filtering the list based on the query, the RecyclerView will be refreshed, and the matching items will be shown at the top of the list.

Explanation:

Now, when you type in the search box, the relevant notes will move to the top of the list.

This looks so unorganised please ignore that and I had tried my best to explain this. Hope you will understand. Thanks

On Thu, Sep 12, 2024, 2:54 AM Rick Hubka @.***> wrote:

I can't seem to get the search functionality to work. I entered and saved about 10 records and that works great. I'm using the latest android studio with a Google Pixel 5 mobile. Any ideas? Thanks

— Reply to this email directly, view it on GitHub https://github.com/Gaurav-Chaudhary1/Quick-Notes-App/issues/1, or unsubscribe https://github.com/notifications/unsubscribe-auth/BG6CUMQUH5FOJY2CP7A4GBDZWCYJ5AVCNFSM6AAAAABOB2LONWVHI2DSMVQWIX3LMV43ASLTON2WKOZSGUZDAOBQGMZDGOI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

rhubka commented 1 month ago

Hi Gaurav Thank you for the fast and detailed response. Unfortunately I am a beginner at Kotlin/SQLite and to do what you say is too difficult for me to understand. Is it possible that you can update your Github code to reflect all these changed? Thanks so much! Rick