Search code examples
androidoptimizationfindpreference

Find Preference Code Optimization


I am writing my first APK and I need some help. I find myself using a lot of similar code to find preference from a preference XML.

Here is a code snippet:

    final Preference backupPref = findPreference("backuptw");
    backupPref.setOnPreferenceClickListener(new OnPreferenceClickListener(){
        public boolean onPreferenceClick(Preference preference) {
            if(preference == backupPref)doBackup();
            return true;
        }
    });
    final Preference restorePref = findPreference("restoretw");
    restorePref.setOnPreferenceClickListener(new OnPreferenceClickListener(){
        public boolean onPreferenceClick(Preference preference) {
            if(preference == restorePref)doRestore();
            return true;
        }
    });
    final Preference tw160Pref = findPreference("tw160");
    tw160Pref.setOnPreferenceClickListener(new OnPreferenceClickListener(){
        public boolean onPreferenceClick(Preference preference) {
            if(preference == tw160Pref)doTW("160");
            return true;
        }
    });
    final Preference tw192Pref = findPreference("tw192");
    tw192Pref.setOnPreferenceClickListener(new OnPreferenceClickListener(){
        public boolean onPreferenceClick(Preference preference) {
            if(preference == tw192Pref)doTW("192");
            return true;
        }
    });
    final Preference tw240Pref = findPreference("tw240");
    tw240Pref.setOnPreferenceClickListener(new OnPreferenceClickListener(){
        public boolean onPreferenceClick(Preference preference) {
            if(preference == tw240Pref)doTW("240");
            return true;
        }
    });
    final Preference restartPref = findPreference("restart");
    restartPref.setOnPreferenceClickListener(new OnPreferenceClickListener(){
        public boolean onPreferenceClick(Preference preference) {
            if(preference == restartPref)doRestart();
            return true;
        }
    });
    final Preference sortPref = findPreference("sort");
    sortPref.setOnPreferenceClickListener(new OnPreferenceClickListener(){
        public boolean onPreferenceClick(Preference preference) {
            if(preference == sortPref)doSort();
            return true;
        }
    });

I am using findPreference a lot of times, which I do not like at all. Can anyone suggest something better?


Solution

  • You can make a HashMap for all your preferences where keys will be names of preferences and values some integer indexes which you will add. E.g.

    final int INDEX_BACKUPTW = 0;
    
    HashMap<String, Integer> mPrefMap = new HashMap<String, Integer>();
    
    mPrefMap.put("backuptw", INDEX_BACKUPTW)
    

    And so on... Then just make only one onClickListener like this:

    OnPreferenceClickListener(){
        public boolean onPreferenceClick(Preference preference) {
            if (preference.hasKey()) {
                int index = mPrefMap.get(preference.getKey());
                switch (index)
                {
                    case INDEX_BACKUPTW:
                       doBackup();
                       break;
                    case INDEX_SOME_OTHER_INDEX_I_HAVE:
                       doWhatever();
                       break;
                    default:
                       Log.e(TAG, "Achtung!");
                }
            }
            return true;
        }
    }
    

    Assign this listener to all your preferences and have fun. In this case your code will not look like it does now.