Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Android
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Tim Cooke
Paul Clapham
Liutauras Vilda
Sheriffs:
Junilu Lacar
Rob Spoor
Jeanne Boyarsky
Saloon Keepers:
Stephan van Hulst
Carey Brown
Tim Holloway
Piet Souris
Bartenders:
Forum:
Android
Kotlin RecyclerView filter search
Maha Sakka
Ranch Hand
Posts: 179
1
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I create an interface with edittext "Search" and "recyclerview" below
<?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" android:background="@color/whiteFour" android:orientation="vertical"> <LinearLayout android:id="@+id/lyt_search" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:orientation="horizontal" android:theme="@style/ThemeOverlay.AppCompat.Light" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.cardview.widget.CardView android:id="@+id/search_bar" android:layout_width="300dp" android:layout_height="wrap_content" android:clipToPadding="false" android:layout_marginStart="20dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" app:cardBackgroundColor="@color/whiteTwo" app:cardCornerRadius="20dp" app:cardElevation="1dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <View android:layout_width="@dimen/spacing_medium" android:layout_height="0dp" /> <ImageView android:id="@+id/bt_mic" android:layout_width="15dp" android:layout_height="15dp" android:layout_gravity="center" android:layout_marginStart="10dp" android:background="@android:color/transparent" android:clickable="true" android:tint="@color/greyishTwo" app:srcCompat="@drawable/ic_search" /> <EditText android:id="@+id/search" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:padding="5dp" android:layout_weight="1" android:background="@android:color/transparent" android:fontFamily="@font/cairo" android:hint="Search..." android:imeOptions="actionSearch" android:maxLines="1" android:singleLine="true" android:textSize="14sp"> </EditText> <View android:layout_width="@dimen/spacing_medium" android:layout_height="0dp" /> </LinearLayout> </androidx.cardview.widget.CardView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:fontFamily="@font/cairo" android:layout_marginTop="15dp" android:layout_marginStart="20dp" android:text="@string/cancel_msg" android:textSize="14sp" /> </LinearLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerViewList" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/lyt_search" /> </androidx.constraintlayout.widget.ConstraintLayout>
I would like to search an item in the recycler view
In my adapter for recyclerview "ItemAdapter" I implement Filterable:
class ItemAdapter(var items: Array<AddressesData>, var mListener: OnItemClickListener) : RecyclerView.Adapter<ItemAdapter.ViewHolder>() , Filterable
and I add the following code in my adapter :
override fun getFilter(): Filter { return filter } private val filter: Filter = object : Filter() { var filteredList: MutableList<AddressesData> = arrayListOf() override fun performFiltering(constraint: CharSequence): FilterResults { if (constraint.isEmpty()) { filteredList.addAll(governments) } else { val filterPattern = constraint.toString().toLowerCase().trim { it <= ' ' } for (item in 0..governments.size) { if (governments[item].englishName!!.toLowerCase().contains(filterPattern)) { filteredList.add(governments[item]) } } } val results = FilterResults() results.values = filteredList return results } override fun publishResults(charSequence: CharSequence?, filterResults: FilterResults) { filteredList = filterResults.values as MutableList<AddressesData> notifyDataSetChanged() } }
In my Activity, I add the following code,
what should I add to doing search in recycler view
Randy Tong
Rancher
Posts: 660
10
I like...
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Use textChangedListener on your search editText.
Life is but a BREATH
Maha Sakka
Ranch Hand
Posts: 179
1
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
As follows, what should I put as code here ?
search.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {} override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { } override fun afterTextChanged(editable: Editable) {} })
Randy Tong
Rancher
Posts: 660
10
I like...
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Have you read
https://inducesmile.com/android-programming/how-to-filter-a-recyclerview-with-a-edittext-in-android/
?
Call getFilter in onTextChanged.
Life is but a BREATH
Maha Sakka
Ranch Hand
Posts: 179
1
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I add the following code :
search.addTextChangedListener(object : TextWatcher { override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {} override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { adapter.filter.filter(charSequence.toString()) } override fun afterTextChanged(editable: Editable) {} })
And this is the adapter code :
class GovernmentSectionItemAdapter(var governments: Array<AddressesData>, var mListener: OnItemClickListener) : RecyclerView.Adapter<GovernmentSectionItemAdapter.ViewHolder>() , Filterable { private var selectedPos = -1 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.item_government_section, parent, false) return ViewHolder(view) } override fun getItemCount() = governments.size override fun onBindViewHolder(holder: ViewHolder, position: Int) { val item: AddressesData = governments[position] holder.tickImage.visibility = (if (selectedPos == position) View.VISIBLE else View.GONE) holder.government.text = item.englishName.toString() holder.itemView.setOnClickListener { selectedPos = position mListener.onItemClick(holder.itemView, item) notifyDataSetChanged() } } inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val government: TextView = itemView.government val tickImage: ImageView = itemView.tickGovernment } interface OnItemClickListener { fun onItemClick(view: View, viewModel: AddressesData) } override fun getFilter(): Filter { return filter } private val filter: Filter = object : Filter() { override fun performFiltering(constraint: CharSequence): FilterResults { var filteredList: MutableList<AddressesData> = arrayListOf() if (constraint.isEmpty()) { filteredList.addAll(governments) } else { val filterPattern = constraint.toString().toLowerCase().trim { it <= ' ' } for (item in 0..governments.size) { if (governments[item].englishName!!.toLowerCase().contains(filterPattern)) { filteredList.add(governments[item]) } } } val results = FilterResults() results.count = filteredList.size results.values = filteredList return results } override fun publishResults(charSequence: CharSequence, filterResults: FilterResults) { governments = filterResults.values as Array<AddressesData> notifyDataSetChanged() } }
But, the filter didn't work up to now
Randy Tong
Rancher
Posts: 660
10
I like...
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Have you tried debug? Did it goes to the else part?
if (constraint.isEmpty()) { filteredList.addAll(governments) } else { val filterPattern = constraint.toString().toLowerCase().trim { it <= ' ' } for (item in 0..governments.size) { if (governments[item].englishName!!.toLowerCase().contains(filterPattern)) { filteredList.add(governments[item]) } } }
Life is but a BREATH
Maha Sakka
Ranch Hand
Posts: 179
1
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I think that the problem is here
override fun publishResults(constraint: CharSequence?, results: FilterResults?) { governments = results?.values as Array<AddressesData> notifyDataSetChanged() }
I tried the debug and it goes to the else part
Randy Tong
Rancher
Posts: 660
10
I like...
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Any exceptions?
What you want to do with this line?
governments = filterResults.values as Array<AddressesData>
Life is but a BREATH
Maha Sakka
Ranch Hand
Posts: 179
1
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I remove this ligne :
governments = filterResults.values as Array<AddressesData>
The recycleview not be updated, always display all the government without filtre
Maha Sakka
Ranch Hand
Posts: 179
1
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I haven't found a solution so far, could you help me please
Randy Tong
Rancher
Posts: 660
10
I like...
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I wish too.
Is it possible for you to upload your project to git so I can help?
Life is but a BREATH
Don't listen to Steve. Just read this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
How To Support Multiple Screen Size In Android
Unable to resume activity - NullPointerException
Problem with the View UI
how to give Edittext and button to half of the screen width?
Kotlin :RecyclerView Swipe to Delete
More...