sharedpreferences保存List集合

                List<MyRecords> listDate=new ArrayList<MyRecords>();
                MyRecords myRecords=new MyRecords(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(date * 1000)),String.valueOf(heart));
                listDate.add(myRecords);
                Gson gson = new Gson();
                String jsonMyRecords = gson.toJson(listDate);
                LogUtil.i(TAG, "json====" + jsonMyRecords);
                DeviceUtil.setPreferences(getApplicationContext(), "jsonMyRecords", jsonMyRecords);

                String jsonMyRecords = DeviceUtil.getPreferences(this, "jsonMyRecords", null);
                if (jsonMyRecords != null) {
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<MyRecords>>(){}.getType();
                    List<MyRecords> listMyRecords = new ArrayList<MyRecords>();
                    listMyRecords = gson.fromJson(jsonMyRecords, type);
                    for(int i = 0; i < listMyRecords.size(); i++)
                    {
                        allRecords.add(listMyRecords.get(i));
                    }
                }

猜你喜欢

转载自blog.csdn.net/qq_34581102/article/details/79861175
今日推荐