【问题标题】:Android, sharedpreferences in AdapterAndroid,适配器中的共享首选项
【发布时间】:2016-04-04 21:04:06
【问题描述】:

在列表适配器 getview 方法中使用 sharedpreferences 的最佳方式是什么? 我必须使用 sharedpreferences 中的一些值,但不知道如何获取它。 我试过了:

pref = getApplicationContext().getSharedPreferences("pref", MODE_PRIVATE);

它在活动中起作用 和:

SharedPreferences pref = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);

在片段中有效,但在 listadapter 中均无效。

【问题讨论】:

  • 在Adapter 中使用时您会看到什么?你用的是什么Context?
  • 解决方法是先使用getContext()

标签: java android android-studio sharedpreferences android-arrayadapter


【解决方案1】:

您可以将Context 作为ListAdapter 的构造函数中的字段传递,并使用context 字段获取SharedPreferences。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 2012-09-03
    • 1970-01-01
    相关资源
    最近更新 更多