Home > Software design >  how to store list of fragments using SharedPreferences
how to store list of fragments using SharedPreferences

Time:12-08

I'm using SharedPreferences to store a list of fragments of my android, however it gave me thousands lines of error which make non-sense:

2021-12-07 17:09:13.228 14833-14833/? E/AndroidRuntime: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:1058) at .......

(just small parts of error messages) I tried to store a list of strings it works fine, but if I switch to an object with list of fragments it fails.

Here is my object.java:

public class CardList {
    ArrayList<Card> list;  //Card is my fragments
    public CardList(ArrayList<Card> list) {
        this.list = list;
    }
    public void updateList(Card c) {
        list.add(c);
    }

    public int getListSize() {
        return list.size();
    }


}

here is my MainActivity.java, the new intent here was just to test if I can get my data back when I'm back to MainActivity

public class MainActivity extends AppCompatActivity {

    public static final String WEATHER_PREFS = "weatherPrefs";
    public CardList cardList;
    public ArrayList<Card> cards;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       
        cards = new ArrayList<>();
        cardList = new CardList(cards);

        SharedPreferences setting = getSharedPreferences(WEATHER_PREFS,MODE_PRIVATE);
        Gson gson = new Gson();
        String savedList = setting.getString("cardList","");

        if (!savedList.equals("")) {
            cardList = gson.fromJson(savedList,CardList.class);
        }


        Button btn= findViewById(R.id.switchActtivity);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                Gson gson = new Gson();

                String savedInstanceString = gson.toJson(cardList);
                SharedPreferences setting = getSharedPreferences(WEATHER_PREFS,MODE_PRIVATE);
                SharedPreferences.Editor editor = setting.edit();
                editor.putString("cardList",savedInstanceString);
                editor.apply();
                startActivity(intent);
            }
        });
        Button increase = findViewById(R.id.increase);
        increase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cardList.updateList(Card.newInstance("hello","world"));
                Log.i("Array List Size After Click",String.valueOf(cardList.getListSize()));
            }
        });


    }
    
}

Can someone teach me how to store custom object with an arraylist attribute in Android? Thanks in advance!

CodePudding user response:

Fragments aren't serializable, meaning they can't be broken down into pure data ie JSON, much less deserializable.

Looking at your code there is no good way to just make it work either.

You need to follow separation of concerns, Fragments are glorified views. They shouldn't be holding information to any great extent, so they shouldn't be passed around as if they did.

What you could do is have a CardModel that contains all information needed for a Card Fragment.

The CardModel can contain simple data that you would find in JSON like Integer,String, etc...

A List<CardModel> can be deserialized from JSON. And then when you're in the MainActivity you could create as many Card Fragments as needed to represent the List<CardModel> with a simple list.size()

  • Related