简体   繁体   English

如何在Android的延迟加载列表中实现本地搜索

[英]How to implement Local search in a lazy loaded list in android

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. 我有一个包含大量数据的列表,因此我实现了延迟加载到列表中,该列表每次向下滑动时都加载10个项目。 But I am stuck in a scenario, ie, 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. 但是我陷入了一个场景,即我一次加载10个项目,但是如果有任何用户想要使用searchview搜索,我必须搜索整个db并准备与此结果相同的列表,并且当searchview被清除时,我必须加载先前加载的列表。 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 谢谢

Consider using Recyclerview. 考虑使用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. 您可以将所有物品(比如说1000件物品)与cardview一起放入recyclerview中。 So would not need to bother about the swipe and load functionality. 因此无需理会滑动和加载功能。 All your items would be stored in the recyclerview adaptor's viewmodel. 您的所有项目都将存储在recyclerview适配器的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 https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

Do one thing Make two more array list you are using for list view items. 做一件事使您要用于列表视图项目的另外两个数组列表。

Let originalArrayList , searchArrayList & tempArrayList 让originalArrayList,searchArrayList和tempArrayList

step 1) Load 10 items in originalArrayList & copy all items in tempArrayList also then notify listview with the originalArrayList. 步骤1)在originalArrayList中加载10个项目并在tempArrayList中复制所有项目,然后还使用originalArrayList通知listview。

step 2) Add text change listener for searchEdittext 步骤2)为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 ie. 步骤3)如果searchEdittext的大小大于1,则在searchEdittext的OntextChanged方法中签入,然后搜索您的数据库并将元素放在第二个arraylist中,即。 searchArrayList and after clearing originalArrayList add all items of searchArrayList into originalArrayList notify adapter searchArrayList并清除originalArrayList后,将searchArrayList的所有项添加到originalArrayList中,通知适配器

step 4) if searchEdittext size is 0 in OntextChanged method then clear originalArrayList add all items of tempArrayList into originalArrayList notify adapter 步骤4)如果OntextChanged方法中searchEdittext的大小为0,则清除originalArrayList,将tempArrayList的所有项添加到originalArrayList中,通知适配器

I hope this will do the trick. 我希望这可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM