【问题标题】:Fill ListPreference contents from Activity从 Activity 填充 ListPreference 内容
【发布时间】:2011-06-21 12:38:35
【问题描述】:

我正在尝试从我的 Activity 中填充我的 ListPreference,而不是将数组存储在 xml 中。但是我发现这有问题。我可能设置错了我的偏好。我可以访问我的 ListPreference 但是我似乎无法动态填充内容。我看过这个帖子。 Is it possible to load ListPreference items from an adapter? 但我似乎无法从中弄清楚。

我正在尝试使用对象列表中的字符串来占用 ListPreference。

这是我目前所拥有的:

    protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    SharedPreferences settings = getSharedPreferences(LUAS_TIMES_PREF, 0);

    ListPreference lp = (ListPreference)findPreference("station_pref");

}

不多,但我怎样才能用我自己的列表填充此列表首选项?

提前致谢。

【问题讨论】:

  • 为什么不想将首选项列表存储在 XML 中?
  • 我有一个 JSON 对象的原始资源,我读入并填写了一个列表。然后,我想使用该列表来占用 ListPreference,因此如果用户偏好启动应用程序来显示默认对象,它将显示默认对象。

标签: java android listpreference


【解决方案1】:

应该很简单:

lp.setEntries(new String [] {"A Man", "A Plan"}); // set displayed text
lp.setEntryValues(new String [] {"a_man", "a_plan"}); // set associated values 

根据需要推断:)

【讨论】:

  • 谢谢,看来问题的根源更深。如果在 LogCat 中打印,我不做什么:06-21 13:13:27.515: ERROR/AndroidRuntime(605): java.lang.IllegalStateException: ListPreference requires an entries array and an entryValues array.
  • 嗨@Hugo 您在评论中提到的 IllegalStateException 的原因和解决方案是什么?
猜你喜欢
  • 2017-04-12
  • 2018-05-28
  • 1970-01-01
  • 2012-08-30
  • 2011-08-20
  • 1970-01-01
  • 2013-05-05
  • 2019-05-07
  • 1970-01-01
相关资源
最近更新 更多