2

I am trying to search a value from database with the search filter. but not updating the searched values in adapter.

Note: loading the data using Paging3 library.

  private val mDiffer: AsyncListDiffer<Data?> = AsyncListDiffer(this, DataComparator)

 object DataComparator: DiffUtil.ItemCallback<Data>() {
        override fun areItemsTheSame(oldItem: Data, newItem: Data): Boolean {
          
            return oldItem.mobileNo == newItem.mobileNo
        }

        override fun areContentsTheSame(oldItem: Data, newItem: Data): Boolean {
            return oldItem == newItem
        }
    }


    fun submitList(list: List<Data?>?)
    {
        mDiffer.submitList(list)

    }
 override fun publishResults(charSequence: CharSequence?, filterResults: FilterResults)
            {
                submitList(filterResults.values as MutableList<Data>)
              

              
                if (filterResults.count > 0) {
                    notifyDataSetChanged();
                }

            }

iam getting the searched values in inmDiffer.submitList(list) method. but values are not added in adapter.

kindly help me to achieve this.

links referred https://howtodoandroid.com/pagination-with-paging-3-android/
link 2 thanks in advance

Kumar
  • 969
  • 2
  • 22
  • 56

1 Answers1

1
 submitList(filterResults.values as MutableList<Data>)

When using DiffUtil, you should submit an immutable list or new list everytime. Otherwise, DiffUtil won't work.

Excerpt from DiffUtil doc:

Note that DiffUtil, ListAdapter, and AsyncListDiffer require the list to not mutate while in use. This generally means that both the lists themselves and their elements (or at least, the properties of elements used in diffing) should not be modified directly. Instead, new lists should be provided any time content changes. It's common for lists passed to DiffUtil to share elements that have not mutated, so it is not strictly required to reload all data to use DiffUtil.

ltp
  • 366
  • 3
  • 10