There is a RecyclerView
that takes data from the server, then stores it in the phone's cache, and then displays it on the device. The problem is that I have 2 identical RecyclerViews
displayed at once, the one that takes data from the Internet and the one that has already saved the data to the cache and displayed it. Can you somehow make the RecyclerView
that takes data from the Internet invisible? Display only the data that was saved in the cache.
Or make it so that the data that is stored in the cache is displayed only if there is no boarding school? I think it will be even better.
Fragment
where it all happens
public class FragmentRink extends Fragment {
List<ItemSkatingRinkSchedule_2> mSkatingRinkScheduleList;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
String URL = "";
String GET_JSON_FROM_SERVER_NUMBER = "session_number";
String GET_JSON_FROM_SERVER_START = "session_start";
String GET_JSON_FROM_SERVER_END = "session_end";
String GET_JSON_FROM_SERVER_WEEKDAYS = "weekdays";
String GET_JSON_FROM_SERVER_WEEKDAYS_AND_HOLIDAYS = "weekends_and_holidays";
RequestQueue requestQueue;
private SwipeRefreshLayout mSwipeRefreshLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OnBackPressedCallback callback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_rink, container, false);
}
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
recyclerView = requireView().findViewById(R.id.recy_srs);
mSkatingRinkScheduleList = new ArrayList<>();
recyclerViewadapter = new AdapterSkatingRinkSchedule_2((ArrayList<ItemSkatingRinkSchedule_2>) mSkatingRinkScheduleList);
recyclerViewlayoutManager = new LinearLayoutManager(getContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(recyclerViewlayoutManager);
recyclerView.setAdapter(recyclerViewadapter);
JSON_DATA_WEB_CALL();
mSwipeRefreshLayout = requireView().findViewById(R.id.ref);
mSwipeRefreshLayout.setColorSchemeResources(R.color.color_8);
mSwipeRefreshLayout.setOnRefreshListener(() -> {
mSkatingRinkScheduleList.clear();
JSON_DATA_WEB_CALL();
mSwipeRefreshLayout.setRefreshing(false);
});
}
public void JSON_DATA_WEB_CALL() {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL, response ->
{
for (int i = 0; i < response.length(); i++) {
try {
ItemSkatingRinkSchedule_2 item = new ItemSkatingRinkSchedule_2();
JSONObject json;
json = response.getJSONObject(i);
item.set_session_number(json.getString(GET_JSON_FROM_SERVER_NUMBER));
item.set_session_start(json.getString(GET_JSON_FROM_SERVER_START));
item.set_session_end(json.getString(GET_JSON_FROM_SERVER_END));
item.set_weekdays(json.getString(GET_JSON_FROM_SERVER_WEEKDAYS));
item.set_weekends_and_holidays(json.getString(GET_JSON_FROM_SERVER_WEEKDAYS_AND_HOLIDAYS));
mSkatingRinkScheduleList.add(item);
}
catch (JSONException e) {
e.printStackTrace();
}
}
recyclerViewadapter.notifyDataSetChanged();
}, error ->
Log.e("Volley", error.toString())) {
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
try {
Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
if (cacheEntry == null) {
cacheEntry = new Cache.Entry();
}
final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
long now = System.currentTimeMillis();
final long ttl = now + cacheExpired;
cacheEntry.data = response.data;
cacheEntry.softTtl = now;
cacheEntry.ttl = ttl;
String headerValue;
headerValue = response.headers.get("Date");
if (headerValue != null) {
cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
headerValue = response.headers.get("Last-Modified");
if (headerValue != null) {
cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
cacheEntry.responseHeaders = response.headers;
final String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONArray(jsonString), cacheEntry);
} catch (UnsupportedEncodingException | JSONException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(JSONArray response) {
super.deliverResponse(response);
}
@Override
public void deliverError(VolleyError error) {
super.deliverError(error);
}
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
return super.parseNetworkError(volleyError);
}
};
requestQueue = Volley.newRequestQueue(requireContext());
requestQueue.add(jsonArrayRequest);
}
}