【问题标题】:Android: Using SharedPreferences with a Tab LayoutAndroid:将 SharedPreferences 与选项卡布局一起使用
【发布时间】:2015-02-19 01:21:38
【问题描述】:

所以我从THIS TUTORIAL 学习了如何使用 FragmentActivity 在 Android 中制作标签。所以现在我有一个包含 3 个选项卡的 FragmentActivity,这意味着这些选项卡中有三个不同的 Fragment。

FragmentActivity 类

public class ProductSingle_Activity extends FragmentActivity {

    ViewPager Tab;
    public TabPagerAdapter TabAdapter;
    ActionBar actionBar;
    Context ctx = this;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.product_single);

.... and so on

我有两个 Fragment 成为 FragmentActivity 的两个选项卡:

一个片段:

public class details_frag_activity extends Fragment {

    Context ctx;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View android = inflater.inflate(R.layout.details_frag, container, false);

    }
}

所以我在应用程序的 SharedPreferences 中存储了一些信息,例如“名称”和“描述”。

SharedPreferences pref = ctx.getSharedPreferences("product_details", 0); 
//ctx being the context
Editor editor = pref.edit();
editor.putString("pname", name);
editor.commit();

我想通过这样的片段之一访问这些信息:

public class details_frag_activity extends Fragment {

    Context ctx;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View android = inflater.inflate(R.layout.details_frag, container, false);

        SharedPreferences pref = ctx.getSharedPreferences("product_details", 0); 
//I cant access the ctx (Context). To avoid the error I just declared a local variable. I need to get the context of the FragmentActivity
        String pname = pref.getString("pname", null);

但是我无法通过片段访问“ctx”(上下文)。有没有办法将 FragmentActivity 的上下文传递给 Fragment 类,以便它们可以从 SharedPreferences 中检索存储的信息?或者有没有更好的方法通过片段(标签)访问公共信息?

【问题讨论】:

  • 是我的答案。

标签: java android android-fragments tabs sharedpreferences


【解决方案1】:

如上一个答案所述,您可以简单地使用:

SharedPreferences pref = getActivity().getSharedPreferences("product_details", 0); 

【讨论】:

    【解决方案2】:

    您可以在details_frag_activity 中覆盖onAttach 方法并在details_frag_activity 类中创建ProductSingle_Activity 对象 用这个...

    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        this.activity = (ProductSingle_Activity) activity;
    }
    

    或者干脆试试

     SharedPreferences pref = getActivity().getSharedPreferences("product_details", 0); 
    

    【讨论】:

      【解决方案3】:

      在片段类中,有一种方法可以通过使用它来获取上下文。

      private Activity activity;
      @Override
      public void onAttach(Activity activity) {
          this.activity = activity;
          super.onAttach(activity);
      }
      

      只需将该方法参数分配给一个字段,然后您就可以在任何需要的地方访问此活动引用作为上下文。因为 Activity 是 Context 的子类。

      【讨论】:

      • 谢谢你成功了! :) 但是你知道如何通过 Shared Preferences 共享一个字符串数组吗?
      • @BenildusRatnayake 请简要说明您的要求?
      • @balajikoduri 我有一个字符串数组。有没有办法通过 SharedPreferences 共享数组?像 putString,也许是 putArray?
      • SharedPreferences 中有一个类似 set 的方法。首选项.edit().putStringSet(arg0, arg1).commit();试试这个。
      • SharedPreferences spf;字符串[] strArr={"一","二","三"};设置 设置=新的 HashSet(); set.addAll(Arrays.asList(strArr)); spf.edit().putStringSet("key", set).commit();
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-15
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多