I am stuck at a problem where I want to update currently displayed listPreference, in "settings" Fragment of my app, after some code run.
Here's what I am trying to do in steps:
- User is shown a settings view
- There's a list preference. When he clicks: (2a) Default Entries/values are shown (from xml file) and it's visible (2b) Some code runs in background and write new values to listPreference (list preference view/dialog is still visible). (2c) Now I want to update this view to show new entries
At (2b) above this is what I am doing:
listPreference = findPreference("mp_key");
listPreference.setEntries(cs);
listPreference.setEntryValues(cs);
//Call on Resume method of the Fragment
Here's what happens. (2a) Default Entries/values are shown (from xml file) and it's visible (2b) Some code runs and write new values to listPreference. list preference view/dialog is still visible. (3a) User "select an item" or "cancel" it, list view disappears and summary is shown (4) When user click on listPreference again, New entries are shown.
//CODE BELOW
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
public class SettingsFragment extends PreferenceFragmentCompat {
private static ListPreference listPreference;
@Override
public void onCreatePreferences(Bundle bundle, String s) {
setPreferencesFromResource(R.xml.preferences, s);
listPreference = findPreference("mp_key"); //in xml resource, not provided here
listPreference.setOnPreferenceClickListener(listPrefClickListener);
}
Preference.OnPreferenceClickListener channelClickListener = new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
CharSequence[] cs = listToCharSequence; //test purposes
listPreference.setEntries(cs);
listPreference.setEntryValues(cs);
return false;
}
};
}
I am not sure what do you mean by "Some code runs in background and write new values to listPreference (list preference view/dialog is still visible)" but added one of my settings fragment code that has a checkbox and a list. Hope this will guide you towards your goal.
}