通过仅第二次工作的代码更改 ListPreference 条目

Changing ListPreference entries by code working only the second time

我想动态更改列表首选项的列表条目和条目值。好吧,终于成功了,列表发生了变化,但问题是只有在首选项屏幕的列表首选项中单击第二次或更多次时,我才能看到新值。

第一次,列表总是原来的列表,从1到10。

        numeroIntentosLP.setOnPreferenceClickListener(new OnPreferenceClickListener()
    {
        @Override
        public boolean onPreferenceClick(Preference preference)
        {

            CharSequence[] NuevosValores = new String[10 - LineaActual];
            int Indice = 0;

            for(int i = LineaActual + 1; i <= 10; i++)
            {
                NuevosValores[Indice++] = String.valueOf(i);
            }

            numeroIntentosLP.setEntries(NuevosValores);
            numeroIntentosLP.setEntryValues(NuevosValores);

            return true;
        }
    });

我试过 numeroIntentosLP.setOnPreferenceChangeListener,但结果相同。 LineaActual 是我从 MainActivity 传递到 PreferenceActivity 的整数值。这对我了解列表的新起始值很有用。

因此,每次我打开首选项屏幕并在列表首选项中第一次单击时,我总是得到数字 1 到 10,但是当我再次单击时,无论点击多少次,我都会得到我想要的,我的意思是从 LineaActual 到 10 的列表。

提前致谢。

好的,我找到了解决方案。就像将代码直接放在 onCreate 中一样简单,而不是在侦听器中。

谢谢samgak,我看到你的回答有点晚了,你是对的,还是谢谢你。