1

我目前正在开发一个应用程序,其中向用户呈现一系列列表,单击每个列表,用户会根据他的选择被带到另一个列表。现在为了记住用户在每个屏幕上所做的事情,我将一系列字符串插入到我的共享首选项中。事实证明,由于我的应用程序有大约 7 8 个选择屏幕,我的整个代码只是与共享首选项中的读取和写入键纠缠在一起。

air_conditioning = Session.getBoolean(SELL_AIR_CONDITIONINING) ? "1" : "0";
        power_steering = Session.getBoolean(SELL_POWER_STEERING) ? "1" : "0";
        power_locks = Session.getBoolean(SELL_POWER_LOCKS) ? "1" : "0";
        power_mirrors = Session.getBoolean(SELL_POWER_MIRROR) ? "1" : "0";
        keyless_entry = Session.getBoolean(SELL_KEYLESS_ENTRY) ? "1" : "0";
        cruise_control = Session.getBoolean(SELL_CRUISE_CONTROL) ? "1" : "0";
        navigation_system = Session.getBoolean(SELL_NAVIGATION_SYSTEM) ? "1" : "0";
        abs = Session.getBoolean(SELL_ABS) ? "1" : "0";
        am_fm_radio = Session.getBoolean(SELL_FM_AM_RADIO) ? "1" : "0";
        cassette_player = Session.getBoolean(SELL_CASSETE_PLAYER) ? "1" : "0";
        cd_player = Session.getBoolean(SELL_CD_PLAYER) ? "1" : "0";
        sun_roof = Session.getBoolean(SELL_SUN_ROOF) ? "1" : "0";
        alloy_rims = Session.getBoolean(SELL_ALLOW_RIMS) ? "1" : "0";

所以这基本上是我的整个代码,看起来很难看,容易出错,而且不太容易维护。我正在寻找在这种情况下对我有用的任何解决方案。

亲切的问候

4

1 回答 1

2

不要使用 SharedPreferences。使用Intent并通过Bundle传递状态。

于 2012-12-12T04:31:01.043 回答