我在不同的Activity中有一个ArrayList,所以我使用了共享首选项来获取它。
SharedPreferences.Editor editor = getActivity().getSharedPreferences("ShoppingListFile", Context.MODE_PRIVATE).edit();
Gson gson = new Gson();
itemList.size();
String json = gson.toJson(itemList);
editor.putString("KEY_ArrayList", json);
editor.apply();
我通过
检索数据后SharedPreferences prefs = getActivity().getSharedPreferences("ShoppingListFile", Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = prefs.getString("KEY_ArrayList", null);
Type type = new TypeToken<ArrayList<Model>>() {}.getType();
ArrayList<Model> arrayList = gson.fromJson(json, type);
最后我把它包含在我的适配器
中mRecyclerView = view.findViewById(R.id.recycler_view1);
mAdapter1 = new RecyclerViewAdapter(arrayList);
LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(manager);
mRecyclerView.setAdapter(mAdapter1);
我的问题是它在我的Recyclerview中没有显示任何项目。