简体   繁体   中英

Changing ListPreference entries by code working only the second time

I want to change dinamically the list entries and entryvalues of a listpreference. Well, finally it works, the list changes, but the problem is that I can see the new values only when I click the second time or more in the listpreference in the preference screen.

The first time, the list is always the original list, from 1 to 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;
        }
    });

I have tried with numeroIntentosLP.setOnPreferenceChangeListener, but the same result. LineaActual is an integer value that I passed from MainActivity to PreferenceActivity. This is useful to me to know the new start value for the list.

So, everytime I open the preference screen and I click for the first time in the listpreference, I always get numbers 1 to 10, but when I click again, no matter how many times, I get what I want, I mean the list from LineaActual to 10.

Thanks in advance.

Ok, I found the solution. It was as easy as put the code directly in the onCreate, not in the listeners.

Thank you samgak, I saw your answer a little bit late and you're right, thank you anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM