[英]Android:Save recyclerview's data to avoid network calls
I have a RecyclerView
in my app.When an item is clicked from RecyclerView
it opens new activity. 我的应用程序中有一个
RecyclerView
。当从RecyclerView
点击一个项目时,它会打开新的活动。
New activity contains two fragments both the fragments loads data from network call. 新活动包含两个片段,两个片段都从网络调用加载数据。 I've used static variables to store data so that when a fragment is opened next time it will not make network call.This works fine but when i click other item from RecyclerView the new activity shows fragment containing data of the previously selected item as the variables are static.
我已经使用静态变量存储数据,以便下次打开片段时不会进行网络调用。这样可以正常工作但是当我从RecyclerView中单击其他项目时,新活动会显示包含先前所选项目数据的片段作为变量是静态的。 How to avoid this?
怎么避免这个?
如果你有id,在那些recyclerview项目的某个地方,你可以做的只是在具有该ID的SharedPreference或SQlite数据库中保存数据,并且每当你需要回调它时,从ID本身调用。
The best would be to use Database, but if you don't want to do that or data is changing after certain time, you can use the LruCache
class provided in android and use item id as key. 最好的方法是使用数据库,但如果您不想这样做或数据在一定时间后发生变化,您可以使用android中提供的
LruCache
类并使用item id作为键。 This gives you control to how you want to store item as well as saving memory by removing least recently used item. 这使您可以控制存储项目的方式以及通过删除最近最少使用的项目来节省内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.