简体   繁体   English

使用活动之间的先前数据重新加载 recyclerView

[英]Reloading recyclerView with previous data between activities

I created an app that has a Leaderboard it in.我创建了一个包含排行榜的应用程序。

The Leaderboard consists of 3 categories: Top rank, Most sharing, Most info.排行榜由 3 个类别组成:排名最高、分享最多、信息最多。

在此处输入图像描述

In each category (tab), there is a recyclerView that loaded 25 results each from firestore .在每个类别(选项卡)中,都有一个 recyclerView,每个从firestore加载 25 个结果。

What happens is that every time a user opens this activity it reads 75 documents which is pretty much and I'm trying to avoid it.发生的情况是,每次用户打开此活动时,它都会读取 75 个文档,这几乎是我试图避免的。

Is there any way that when a user starts the activity for the first time it will load these 75 documents but if he starts the activity 5 minutes later, for example, it will just display the same results as previously without reading 75 documents from firestore again?有没有办法让用户第一次启动活动时会加载这 75 个文档,但如果他在 5 分钟后启动活动,例如,它将只显示与以前相同的结果,而无需再次从firestore读取 75 个文档?

Each recyclerView is loaded by using:每个 recyclerView 使用以下方法加载:

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager( getApplicationContext() );
rv_TopRank.setLayoutManager( mLayoutManager );
rv_TopRank.setItemAnimator( new DefaultItemAnimator() );

private void showTopRanked() {
    usersID.clear();
    usersName.clear();
    usersResult.clear();

    db.collectionGroup( "UserData" ).orderBy( "rank", Query.Direction.DESCENDING ).limit( AppConstants.LEADERBOARD_TO_LOAD ).get()
            .addOnSuccessListener( queryDocumentSnapshots -> {
                for (QueryDocumentSnapshot document : queryDocumentSnapshots) {
                    if (document != null) {
                        usersID.add( document.getId() );
                        usersName.add( document.getString( "username" ) );
                        usersResult.add( document.getDouble( "rank" ) );
                    }
                }
                leaderBoardAdapter = new LeaderBoardAdapter( usersID, usersName, usersResult, 0 );
                rv_TopRank.setAdapter( leaderBoardAdapter );
                leaderBoardAdapter.notifyDataSetChanged();
            } );
}

Thank you谢谢

One option would be to use Android's Shared Preferences to save this data locally, and update it only when you deem it necessary as opposed to every time the app is opened.一种选择是使用 Android 的Shared Preferences将这些数据保存在本地,并仅在您认为有必要时更新它,而不是每次打开应用程序时更新。

You can use this approach:您可以使用这种方法:

In the onCreate() method of the activity, read the date from Calendar and save it in sharedPreferences as the date of last update and then check to see if the current date is not equal to the date of last update before you query from firebase.在活动的onCreate()方法中,从Calendar中读取日期并保存在sharedPreferences中作为最后更新的日期,然后在从firebase查询之前检查当前日期是否不等于最后更新的日期。

Something like this:像这样的东西:

val sharedPref = getSharedPreferences("SharedPref", Context.MODE_PRIVATE)

val today = Calendar.getInstance().get(Calendar.DAY_OF_YEAR)
val lastUpdateDay = sharedPref.getInt("lastUpdateDay", 0)

if (today != lastUpdateDay) {
    //THE LOCAL DATA IS OLD SO IT NEEDS TO BE UPDATED FROM FIREBASE THEREFORE:
   
    //Query and show Firebase database data
    //Store the data locally (Either in a database or again in sharedPref)
    //and save `today` to sharedPref in the onSuccess() block like so:
    sharedPref.edit().putInt("lastUpdateDay", today).apply()
} else {
    //THE LOCAL DATA IS UP-TO-DATE SO NO NEED FOR A FIREBASE QUERY

    //Load the data you have stored locally the day before
}

I'm sorry the code is in Kotlin.对不起,代码在 Kotlin 中。 it's just to give you an idea.这只是给你一个想法。

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

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