我怎么可以存储在共享preferences整数数组? [英] How can I store an integer array in SharedPreferences?

查看:138
本文介绍了我怎么可以存储在共享preferences整数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存/使用共享preferences记得一个整数数组,这可能吗?

I want to save/recall an integer array using SharedPreferences, is this possible?

推荐答案

您可以尝试做这种方式:

You can try to do it this way:

  • 把你的整数转换成字符串,一个字符分隔每个INT,例如一个逗号,然后将其保存为一个字符串:

  • Put your integers into a string, delimiting every int by a character, for example a comma, and then save them as a string:

SharedPreferences prefs = getPreferences(MODE_PRIVATE);
int[] list = new int[10];
StringBuilder str = new StringBuilder();
for (int i = 0; i < list.length; i++) {
    str.append(list[i]).append(",");
}
prefs.edit().putString("string", str.toString());

  • 获取字符串和使用的StringTokenizer解析:

  • Get the string and parse it using StringTokenizer:

    String savedString = prefs.getString("string", "");
    StringTokenizer st = new StringTokenizer(savedString, ",");
    int[] savedList = new int[10];
    for (int i = 0; i < 10; i++) {
        savedList[i] = Integer.parseInt(st.nextToken());
    }
    

  • 这篇关于我怎么可以存储在共享preferences整数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆