简体   繁体   English

Android:保存recyclerview的数据以避免网络呼叫

[英]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.

相关问题 如何避免RecyclerView中的数据重复 - Android? - How to avoid data repetition in RecyclerView - Android? 如何在 Android recyclerview 上的共享首选项中保存和加载数据 - How to save and load data in sharedpreferences on an Android recyclerview 在哪里进行网络调用以下载JSON for RecyclerView - Where to do network calls to download JSON for RecyclerView 如何在旋转期间保存RecyclerView.Adapter的数据? - How to save RecyclerView.Adapter's data during rotation? 如何保存recyclerview LiveData的数据<list<sportdata> &gt; 避免底部导航数据丢失? </list<sportdata> - How to save the data of recyclerview LiveData<List<SportData>> to avoid the loss of bottom navigation data? Android RxJava / Kotlin-将大量数据映射到网络调用 - Android RxJava/Kotlin - map large set of data to network calls 如何选择/强制使用移动数据(或wifi)在Android上进行网络通话? - How to choose/enforce using mobile data (or wifi) for network calls on Android? 无法在RecyclerView Android中使用对象序列化器保存或检索数据 - Can't Save or Retrieve Data using Object Serializer in RecyclerView Android Android:如何保存数据(意图数据) - Android : How to save data (Intent's Data) Android:处理同步网络呼叫 - Android: Handling synchronous network calls
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM