0

So basically I have a PreferenceActivity that I use to make calls to a Web Service through private classes extending AsyncTask. Whenever a Preference changes I have a "huge" "switch case" determining which preference has been changed and then makes the call accordingly.

Now I have two questions :

  1. This seems like a "silly" way to deal with my problem. Do you have any suggestions as to what I should do instead?
  2. I just made another set of preferences consisting of N CheckboxPreferences. How do I "deal" with these in terms of calling the class JoinQueueTask().execute(String queue_key) (JoinQueue extends AsyncTask) ?

Relevant Code snippet :

    public void onSharedPreferenceChanged(SharedPreferences arg0, String key) 
{
    if(isFirstRun)
        return;

    // Call Forward Preferences
    if(key.contentEquals("call_forward_always"))
    {
        cfInfo[0] = "1";
        cfInfo[1] = arg0.getString(key, "ERROR");
        new PushCallForwardInfoTask().execute(cfInfo);
        ep1.setSummary("Viderestiller til " + cfInfo[1]);
    }
    else if(key.contentEquals("call_forward_busy"))
    {
        cfInfo[2] = "1";
        cfInfo[3] = arg0.getString(key, "ERROR");
        new PushCallForwardInfoTask().execute(cfInfo);
        ep2.setSummary("Viderestiller til " + cfInfo[3]);
    }
    else if(key.contentEquals("call_forward_noresponse"))
    {
        cfInfo[4] = "1";
        cfInfo[5] = arg0.getString(key, "ERROR");
        new PushCallForwardInfoTask().execute(cfInfo);
        ep3.setSummary("Viderestiller til " + cfInfo[5]);
    }
    else if(key.contentEquals("call_forward_timeout"))
    {
        cfInfo[6] = arg0.getString(key, "ERROR");
        new PushCallForwardInfoTask().execute(cfInfo);
        ep4.setSummary("Viderestiller efter " + cfInfo[6] + " sekunder");
    }

    // Show Number Preferences
    else if(key.contentEquals("shownumber_list"))
    {
        String[] newnumber = {""}; 
        newnumber[0] = arg0.getString(key, "ERROR");
        new PushNumberTask().execute(newnumber);
        lp.setSummary(arg0.getString(key, "ERROR"));
    }

    // Voicemail Preferences
    else if(key.contentEquals("voicemail_checkbox"))
    {
        final Boolean[] vmStatus = { Boolean.FALSE };
        vmStatus[0] = cp.isChecked();
        new PushVoicemailStatus().execute(vmStatus);
    }   

}
CodePrimate
  • 6,646
  • 13
  • 48
  • 86

1 Answers1

0

To answer your first question, if you don't want to deal with using a big switch/case statement your best bet would be to extends the preferences you are using. For instance if you are using a ton of checkbox's just extend the checkbox preference and add your logic to overridden methods. In my experience this cuts way back on code and makes the logic very easy to follow. You can see the original source at grepcode.com if you need to understand how something is working or need to "hack" something in.

jjNford
  • 5,170
  • 7
  • 40
  • 64
  • Alright. Are you aware of whether or not my current solution is bad practice ? – CodePrimate Mar 15 '12 at 12:53
  • @litemode Umm.. I wouldn't say it is bad practice. Android makes it hard to handle this situation because the preference changes usually are Strings (when the SDK moves to JRE7 you can switch strings). It is good practice to keep your code loosely coupled and I think handling the preferences change the in preference item does just this. That way if a preference is added or removed... it happens in just the one place. But there are situation where what you are doing is necessary and the only way available, an what you are comfortable with and find easier to read and maintain. – jjNford Mar 15 '12 at 13:09