1

重複の可能性:
AndroidのSharedPreferencesに配列またはオブジェクトを追加することは可能ですか?

共有設定を使用して目覚まし時計に複数のアラームを作成しました。特定の曜日にアラームを鳴らしたいので、アラームを鳴らす日を節約できるアレイが必要です。誰もが共有設定に配列を配置する方法とそれを再度取得する方法を教えてもらえますか..?

SharedPreferences set=getSharedPreferences(MYPR, 0);
            SharedPreferences.Editor ed=set.edit();
            ed.putInt("Ahh", Ahh);
            ed.putInt("Amm",Amm);
            ed.putBoolean("en", true);
            ed.commit();

今、私は目覚まし時計とそのプロパティを共有設定に入れています。配列(曜日)を置くために何を追加するのか..?

4

1 に答える 1

5

書くには、

SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(this);
    JSONArray jsonArray = new JSONArray();
    jsonArray.put(1);
    jsonArray.put(2);
    Editor editor = prefs.edit();
    editor.putString("key", jsonArray.toString());
    System.out.println(jsonArray.toString());
    editor.commit();

読むには、

try {
        JSONArray jsonArray2 = new JSONArray(prefs.getString("key", "[]"));
        for (int i = 0; i < jsonArray2.length(); i++) {
             Log.d("your JSON Array", jsonArray2.getInt(i)+"");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
于 2013-01-28T07:17:01.360 に答える