From bb4598eb1a5c7fb30a46ffc818bb031f8eae3f63 Mon Sep 17 00:00:00 2001 From: midorikocak Date: Tue, 24 Oct 2017 18:21:25 +0200 Subject: [PATCH] automati currenciesc --- .../CurrencyFragment.java | 5 ++-- .../CurrencySettingsActivity.java | 26 ++++++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/mynameismidori/currencypickerexample/CurrencyFragment.java b/app/src/main/java/com/mynameismidori/currencypickerexample/CurrencyFragment.java index 72ec599..1214a7c 100644 --- a/app/src/main/java/com/mynameismidori/currencypickerexample/CurrencyFragment.java +++ b/app/src/main/java/com/mynameismidori/currencypickerexample/CurrencyFragment.java @@ -56,7 +56,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa mTextView = (TextView) view.findViewById(R.id.selectedCurrencyPreference); preferences = PreferenceManager.getDefaultSharedPreferences(getContext()); preferences.registerOnSharedPreferenceChangeListener(this); - mCurrencyPicker.setCurrenciesList(preferences.getStringSet("selectedCurrencies", new HashSet())); + + String selectedCurrency = preferences.getString("selectedCurrency", getString(R.string.default_currency)); mTextView.setText(selectedCurrency); @@ -71,7 +72,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa // and decide, in which order they will be displayed //Collections.reverse(nc); mCurrencyPicker.setCurrenciesList(nc); - + mCurrencyPicker.setCurrenciesList(preferences.getStringSet("selectedCurrencies", new HashSet())); mCurrencyPicker.setListener(this); } diff --git a/app/src/main/java/com/mynameismidori/currencypickerexample/CurrencySettingsActivity.java b/app/src/main/java/com/mynameismidori/currencypickerexample/CurrencySettingsActivity.java index d311399..bbd3351 100644 --- a/app/src/main/java/com/mynameismidori/currencypickerexample/CurrencySettingsActivity.java +++ b/app/src/main/java/com/mynameismidori/currencypickerexample/CurrencySettingsActivity.java @@ -19,14 +19,13 @@ public class CurrencySettingsActivity extends Activity { - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getFragmentManager().beginTransaction().replace(android.R.id.content, new CurrencyPreferenceFragment()).commit(); - } + } - public static class CurrencyPreferenceFragment extends PreferenceFragment implements CurrencyPickerListener { + public static class CurrencyPreferenceFragment extends PreferenceFragment implements CurrencyPickerListener, SharedPreferences.OnSharedPreferenceChangeListener { SharedPreferences preferences; private CurrencyPicker mCurrencyPicker; private CurrencyPreference currencyPreference; @@ -41,7 +40,7 @@ public void onCreate(Bundle savedInstanceState) { addPreferencesFromResource(R.xml.preferences); preferences = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); currencyPreference = (CurrencyPreference) findPreference("selectedCurrency"); - currencyPreference.setCurrenciesList(preferences.getStringSet("selectedCurrencies",new HashSet())); + currencyPreference.setCurrenciesList(preferences.getStringSet("selectedCurrencies", new HashSet())); mCurrencyPicker = CurrencyPicker.newInstance("Select Currency"); multiSelectListPreference = (MultiCurrencyPreference) findPreference("selectedCurrencies"); @@ -68,6 +67,25 @@ public void onCreate(Bundle savedInstanceState) { mCurrencyPicker.setListener(this); } + @Override + public void onResume() { + preferences.registerOnSharedPreferenceChangeListener(this); + super.onResume(); + } + + @Override + public void onPause() { + preferences.registerOnSharedPreferenceChangeListener(this); + super.onPause(); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if (key.equals("selectedCurrencies")) { + currencyPreference.setCurrenciesList(preferences.getStringSet("selectedCurrencies", new HashSet())); + } + } + @Override public void onSelectCurrency(String name, String code, String symbol, int flagDrawableResID) { currencyPreference.setValue(code);