PreferenceActivity: using onSharedPreferenceChanged()

泪湿孤枕 提交于 2019-12-02 21:35:13

问题


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);
    }   

}

回答1:


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.



来源:https://stackoverflow.com/questions/9719541/preferenceactivity-using-onsharedpreferencechanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!