How to implement Local search in a lazy loaded list in android

615 Views Asked by At

I have a list which contains plenty of data so I have implemented the lazy loading in to the list which loads 10 items for each swipe down. But I am stuck in a scenario, i.e, I am loading 10 items at a time but If any user wants to search using searchview I have to search entire db and prepare the same list with this results and when the searchview is cleared I have to load the previously loaded list. Can anybody help me to solve this. I have not posted the code because of security reasons. Kindly please suggest a way to solve this.

Thanks

2

There are 2 best solutions below

2
Dilberted On

Consider using Recyclerview. It would improve the performance on the device even if there are huge number of items.

You can place all your items (lets say 1000 items) in the recyclerview in conjunction with cardview. So would not need to bother about the swipe and load functionality. All your items would be stored in the recyclerview adaptor's viewmodel. You can perform the search within the adaptor and display the filtered results (assuming the data in the list is not exceptionally huge).

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

0
Akash Jindal On

Do one thing Make two more array list you are using for list view items.

Let originalArrayList , searchArrayList & tempArrayList

step 1) Load 10 items in originalArrayList & copy all items in tempArrayList also then notify listview with the originalArrayList.

step 2) Add text change listener for searchEdittext

step 3) Check in OntextChanged method of searchEdittext if size of searchEdittext if greater than 1 then search your db and put the elemnts in second arraylist i.e.. searchArrayList and after clearing originalArrayList add all items of searchArrayList into originalArrayList notify adapter

step 4) if searchEdittext size is 0 in OntextChanged method then clear originalArrayList add all items of tempArrayList into originalArrayList notify adapter

I hope this will do the trick.