Android SharedPreference值会自动加载

时间:2017-07-06 09:48:47

标签: android sharedpreferences

在下面编写的代码中,每当我的购物车活动启动时以及编译器到达时:HashMap<String, String> cart = session2.getUserDetails();然后它默认加载默认项值而不是加载空值加载的值是先前给出的值跑。任何人都可以请求我在我的代码中出错吗?

public class Cart extends AppCompatActivity {

ListView lvCartItems;
Button bClear;
Button bShop;
TextView tvTotalPrice;

// Session Manager Class
ProductSessionManager session2;

ArrayList<CartItem> cartItems = new ArrayList<CartItem>();
Context context = Cart.this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_cart);

    textHome = (TextView) findViewById(R.id.text_home);
    textSchedules = (TextView) findViewById(R.id.text_account);
    textMusic = (TextView) findViewById(R.id.text_cart);
    tvTotalPrice=(TextView)findViewById(R.id.tvTotalPrice);

    lvCartItems = (ListView) findViewById(R.id.lvCartItems);
    LayoutInflater layoutInflater = getLayoutInflater();


    // Session class instance
    session2 = new ProductSessionManager(this);


    // get user data from session
    HashMap<String, String> cart = session2.getUserDetails();

    // item
    String item1 = cart.get(ProductSessionManager.KEY_item1);
    //quantity
    String quantity1 = cart.get(ProductSessionManager.KEY_quantity1);
    //rate
    String rate1 = cart.get(ProductSessionManager.KEY_rate1);

1 个答案:

答案 0 :(得分:0)

使用此选项清除会话。

 SharedPreferences pref  = 
 context.getSharedPreferences("shared_preferences_name", "mode");   
 SharedPreferences.Editor editor = pref.edit();
 editor.clear();
 editor.commit();