1

In my application, I use this class as a model:

class ExpenseItem (val concept: String, val amount: String, val months: List<String>, val type: Type, val cards_image: Int, val payDay: Int, val notes: String) {

    enum class Type {RECURRENT, VARIABLE}
}

And with this model, I create a mutable list

var generalExpensesList: MutableList<ExpenseItem> = mutableListOf()

and I add items

 val currentExpense = ExpenseItem(
                    concept,
                    amount,
                    listOfMonths,
                    enumtype,
                    card_image_number,
                    payday.toInt(),
                    notes
                )

                generalExpensesList.add(currentExpense)

As you can see, one of the model fields is also a String type list, in case it was important

Well, my intention is to convert this list to String, save it as a sharedpreference, and then create a new list using that String retrieved from the sharedpreference. To convert the list to String I can use toString or joinToString, both give me an optimal result. I have the problem when I want to create a new list from the String. I can do it with lists of type List<String>, but never with lists of type List<ExpenseItem>

Can someone help me with this?

4
  • 1
    Using Gson map it to Json and save to SharePreference, and you can easy to retrieved List<ExpenseItem> Commented May 27, 2020 at 8:28
  • You mean the GSON library, right? I have never used it. I will have to investigate to see how it works. Would you know of any implementation example applied to this?. Thanks Commented May 27, 2020 at 8:32
  • For instance you have in you sharedPrefs [one, two]. Do you want to convert this content form String to List<String>? Commented May 27, 2020 at 8:34
  • no, the sharedprefs is a string with the data of the model class "ExpenseItem" and that String is what I want to convert to a list of type List<ExpenseItem> Commented May 27, 2020 at 8:37

2 Answers 2

2

Simple way, you can use Gson library, add it to build.gradle, it will serialize your list to JSON and save it to SharePreference

  implementation 'com.google.code.gson:gson:2.8.6'
    public void saveItems(List<ExpenseItem> items) {
        if (items != null && !items.isEmpty()) {
            String json = new Gson().toJson(items);
            mSharedPreferences.edit().putString("items", json).apply();
        }
    }

    public List<ExpenseItem> getItems() {
        String json = mSharedPreferences.getString("items", "");
        if (TextUtils.isEmpty(json)) return Collections.emptyList();
        Type type = new TypeToken<List<ExpenseItem>>() {
        }.getType();
        List<ExpenseItem> result = new Gson().fromJson(json, type);
        return result;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

You need to use Gson() class. First Make class like below

class WhateverName(var generalExpensesList: MutableList<ExpenseItem> = mutableListOf()) 

and pass your list to this class and make a object of this class and then need to make string of it like below

Gson().toJson(WhateverName(arrayListOf()))

it will give you the string and save it to preference as a string. After retrieve string from preference you need convert it to that object again for doing that use below code.

Gson().fromJson("string", WhateverName::class.java)

it will give you the class of WhateverName, you can access your list from it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.