簡體   English   中英

Android:將地圖保存到SharedPreferences嗎?

[英]Android: Save map to SharedPreferences?

我需要以這樣的方式將數據保存到SharedPreferences中:

{
    "days": [
        {
            "exercises": [
                {
                    "name": "Bench Press",
                    "sets": 3,
                    "reps": 8
                },
                {
                    "name": "Skull Crushers",
                    "sets": 3,
                    "reps": 8
                },
                {
                    "name": "Flys",
                    "sets": 3,
                    "reps": 8
                }
            ]
        },
        {
            "exercises": [
                {
                    "name": "Bench Press",
                    "sets": 3,
                    "reps": 8
                },
                {
                    "name": "Skull Crushers",
                    "sets": 3,
                    "reps": 8
                },
                {
                    "name": "Flys",
                    "sets": 3,
                    "reps": 8
                }
            ]
        }
    ]
}

我將需要從對象中拉出並添加到對象中。 我知道您無法將地圖保存到SharedPreferences 我開始認為我最好的選擇是使用ObjectOutputStream但是我不確定這是否是使用內部存儲器的最好選擇。 我想我只是在尋找有關最佳選擇的指導。

編輯:從Advice-Dog所說的話,我認為我最好的選擇是使用gson 這是否意味着當我想(例如)向“天”的第二個索引添加另一個練習時,我將首先從首選項中獲取對象,將其從gson轉換為對象,然后添加練習,然后將其轉換回到gson ,然后覆蓋首選項? 我並不是說這很不好,我只是想知道這是應該做的,還是可取的。

在Android中保存更復雜的類型時,建議使用gson Gson是Google的JSON解析庫,即使您不使用JSON,也可以將Objects轉換為JSON String,並輕松存儲它。

例如,您可以像這樣將Objects列表轉換為String

val list : List<MyObject>  // ... add items to your list

// Convert to JSON

val string = gson.toJson(list)

// Store it into Shared Preferences
preferences.putString("list", string).apply()

然后您可以輕松地將其重新放入這樣的列表中。

// Fetch the JSON 

val string = preferences.getString("list", "")

// Convert it back into a List

val list: List<MyObject> = gson.fromJson(string, object : TypeToken<List<MyObject>>() {}.type)

使用JSON是最好的解決方案,但這是另一種解決方案(只是添加另一種解決問題的方法):

您可以使用共享首選項鍵,如下所示:

for((dayIndex, day) in days.withIndex)
    for((exeIndex, exe) in day)
       for((key, value) in exe)
          addExercise(day = dayIndex, exercise = exeIndex, key = key, value = value)


fun addExercise(day: String, exercise: String, key: String, value: String) = 
     preferences.putString("day${day}_exe${exercise}_${key}", value).apply()           

那么您可以從第二天開始獲得第一個練習名稱 ,如下所示:

val first_exercise_second_day = preferences.getString("day2_exe1_name")

您可以像這樣在第三天添加練習:

 addExercise(day = "3", exercise = "1", key = "name", value = "Flys")
 addExercise(day = "3", exercise = "1", key = "sets", value = "5")
 addExercise(day = "3", exercise = "1", key = "reps", value = "3")

但是要找到空的日期或空的運動編號,您必須獲取共享首選項的所有鍵並使用正則表達式或循環

您可以將庫用於google ==> Gson在gradle的依賴項中添加它

 implementation 'com.google.code.gson:gson:2.8.0' 

您可以將此庫將對象作為String轉換為json,然后可以將其保存在共享的Pref As String中,當從Pref獲取字符串時,可以將String轉換為對象

將json轉換為模型

    /*type of model*/=new Gson().fromJson(/*json string*/,/*type of model*/)

將模型轉換為JSON作為字符串

 new Gson().toJson(/*model you want to convert */)===> return String

必須為json建模相同的層次結構,在轉換過程中會出現問題

json的模型必須類似於此模型

  public class JSONModel implements Serializable {


    @SerializedName("days")
    private List<Day> mDays;

    public List<Day> getDays() {
        return mDays;
    }

    public void setDays(List<Day> days) {
        mDays = days;
    }

    public class Day {

        @SerializedName("exercises")
        private List<Exercise> mExercises;

        public List<Exercise> getExercises() {
            return mExercises;
        }

        public void setExercises(List<Exercise> exercises) {
            mExercises = exercises;
        }

    }
    public class Exercise {

        @SerializedName("name")
        private String mName;
        @SerializedName("reps")
        private Long mReps;
        @SerializedName("sets")
        private Long mSets;

        public String getName() {
            return mName;
        }

        public void setName(String name) {
            mName = name;
        }

        public Long getReps() {
            return mReps;
        }

        public void setReps(Long reps) {
            mReps = reps;
        }

        public Long getSets() {
            return mSets;
        }

        public void setSets(Long sets) {
            mSets = sets;
        }

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM