save arraylist in shared preference

后端 未结 5 1254
抹茶落季
抹茶落季 2020-12-06 15:32

I am storing values in ArrayList and pass it to using bundle to next Fragment, and there I set values to my TextView, till here it wor

5条回答
  •  执念已碎
    2020-12-06 16:13

    An alternative solution (just thought of it):

    If you have an array called addtos and you want to add the array to shared preferences, since the variable is represented in the dictionary as a String, you could append the array index to the end of that string.

    e.g -

    Storing

    for(int i = 0; i

    Receiving

    int i = 0;
    while(true){
        if(prefs.getString("addtos"+i, "")!=""){  // or whatever the default dict value is
            // do something with it
            i++;
        }else{break;}
    }
    

    Seems ok to me, if anyone sees a problem with this, let me know.

    Also, no need for ArrayLists

提交回复
热议问题