在我的 RecyclerView
在上显示数据库中的数据 phpMyAdmin
. 当我第一次进入应用程序时,我需要将数据加载并保存到手机上,然后我可以完全关闭应用程序,下一次我在没有互联网的情况下进入应用程序时,上次加载的数据是在仍然有互联网的情况下加载的,但是如果出现了互联网,我可以使用 SwipeRefreshLayout
,并保存了此数据的最新版本。
试图用 SharedPreferences
,但由于某种原因它不起作用。我去应用程序,没有数据显示
我写了这样一个保存函数,可能有拼写错误,帮我弄清楚。
private void saveData() {
SharedPreferences sharedPreferences = this.requireActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(mSkatingRinkScheduleList);
editor.putString("task list", json);
editor.apply();
}
private void loadData() {
SharedPreferences sharedPreferences = this.requireActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("task list", null);
Type type = new TypeToken<ArrayList<ItemSkatingRinkSchedule_2>>() {
}.getType();
mSkatingRinkScheduleList = gson.fromJson(json, type);
if (mSkatingRinkScheduleList == null) {
mSkatingRinkScheduleList = new ArrayList<>();
}
}
所有内容所在的java文件
public class FragmentRink extends Fragment {
private RecyclerView mRec_srs;
private AdapterSkatingRinkSchedule adapter_srs;
private ArrayList<ItemSkatingRinkSchedule> exampleList;
private RecyclerView.LayoutManager mLayoutManager;
List<ItemSkatingRinkSchedule_2> mSkatingRinkScheduleList;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayoutManager;
RecyclerView.Adapter recyclerViewadapter;
String HTTP_JSON_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";
JsonArrayRequest jsonArrayRequest;
TextView save;
RequestQueue requestQueue;
View ChildView;
int GetItemPosition;
ArrayList<String> SubjectNames;
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) {
loadData();
recyclerView = requireView().findViewById(R.id.recy_srs);
recyclerView.setHasFixedSize(true);
recyclerViewlayoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(recyclerViewlayoutManager);
SubjectNames = new ArrayList<>();
JSON_DATA_WEB_CALL();
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
final GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return true;
}
});
@Override
public boolean onInterceptTouchEvent(@NonNull RecyclerView Recyclerview, @NonNull MotionEvent motionEvent) {
ChildView = Recyclerview.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
if (ChildView != null && gestureDetector.onTouchEvent(motionEvent)) {
GetItemPosition = Recyclerview.getChildAdapterPosition(ChildView);
}
return false;
}
@Override
public void onTouchEvent(@NonNull RecyclerView Recyclerview, @NonNull MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
mSwipeRefreshLayout = requireView().findViewById(R.id.ref);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
JSON_DATA_WEB_CALL();
mSwipeRefreshLayout.setRefreshing(false);
saveData();
}
});
}
private void saveData() {
SharedPreferences sharedPreferences = this.requireActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(mSkatingRinkScheduleList);
editor.putString("task list", json);
editor.apply();
}
private void loadData() {
SharedPreferences sharedPreferences = this.requireActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("task list", null);
Type type = new TypeToken<ArrayList<ItemSkatingRinkSchedule_2>>() {
}.getType();
mSkatingRinkScheduleList = gson.fromJson(json, type);
if (mSkatingRinkScheduleList == null) {
mSkatingRinkScheduleList = new ArrayList<>();
}
}
public void JSON_DATA_WEB_CALL() {
jsonArrayRequest = new JsonArrayRequest(HTTP_JSON_URL,
this::JSON_PARSE_DATA_AFTER_WEBCALL,
error -> {
});
requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(jsonArrayRequest);
}
public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array) {
mSkatingRinkScheduleList.clear();
for (int i = 0; i < array.length(); i++) {
ItemSkatingRinkSchedule_2 GetDataAdapter2 = new ItemSkatingRinkSchedule_2();
JSONObject json;
try {
json = array.getJSONObject(i);
GetDataAdapter2.set_session_number(json.getString(GET_JSON_FROM_SERVER_NUMBER));
GetDataAdapter2.set_session_start(json.getString(GET_JSON_FROM_SERVER_START));
GetDataAdapter2.set_session_end(json.getString(GET_JSON_FROM_SERVER_END));
GetDataAdapter2.set_weekdays(json.getString(GET_JSON_FROM_SERVER_WEEKDAYS));
GetDataAdapter2.set_weekends_and_holidays(json.getString(GET_JSON_FROM_SERVER_WEEKDAYS_AND_HOLIDAYS));
SubjectNames.add(json.getString(GET_JSON_FROM_SERVER_NUMBER));
SubjectNames.add(json.getString(GET_JSON_FROM_SERVER_START));
SubjectNames.add(json.getString(GET_JSON_FROM_SERVER_END));
SubjectNames.add(json.getString(GET_JSON_FROM_SERVER_WEEKDAYS));
SubjectNames.add(json.getString(GET_JSON_FROM_SERVER_WEEKDAYS_AND_HOLIDAYS));
} catch (JSONException e) {
e.printStackTrace();
}
mSkatingRinkScheduleList.add(GetDataAdapter2);
}
recyclerViewadapter = new AdapterSkatingRinkSchedule_2((ArrayList<ItemSkatingRinkSchedule_2>) mSkatingRinkScheduleList);
recyclerView.setAdapter(recyclerViewadapter);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!