首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将数据存储在一个片段中以获取另一个片段中的数据?

如何将数据存储在一个片段中以获取另一个片段中的数据?
EN

Stack Overflow用户
提问于 2019-06-26 08:19:24
回答 3查看 124关注 0票数 0

我在共享首选项类中存储了一个字符串,并希望在另一个片段中使用该字符串。

我想我在上下文,应用程序上下文上做了一些错误的事情。我不知道上下文的事情。每当我打开我的个人资料片段,应用程序就会崩溃。

共享首选项类

代码语言:javascript
复制
public class SharedPrefs {
private static final String myPrefs = "myprefs";
private static final String LogedInKey = "Key";
private final Context context;
private final SharedPreferences sharedPreferences;
private final SharedPreferences.Editor editor;


public SharedPrefs(Context context) {
    this.context = context;
    sharedPreferences = context.getSharedPreferences(myPrefs, Context.MODE_PRIVATE);
    editor = sharedPreferences.edit();
}

public void savePref(String key) {
    editor.putString(LogedInKey, key);
    editor.commit();
}

public String getLogedInKey() {
    return sharedPreferences.getString(LogedInKey, null);
}

public void clearLogin() {
    editor.putString(LogedInKey, null);
    editor.commit();
}}

我的个人资料片段:

代码语言:javascript
复制
public class ProfileFragment extends Fragment {
private Context context;
private SharedPrefs sharedPrefs=new SharedPrefs(context);

public ProfileFragment(Context context) {
    this.context = context;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_profile, null);
    TextView userName=view.findViewById(R.id.userName);
    String a=sharedPrefs.getLogedInKey();
    userName.setText(a);

    return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


}}

我只想将字符串设置为共享引用中的TextView。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-06-26 08:39:36

1.实际上,您不需要将上下文作为片段构造函数传递,因为这是不正确的,它将无法工作

代码语言:javascript
复制
    public class ProfileFragment extends Fragment {
    private SharedPrefs sharedPrefs;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
    sharedPrefs=new SharedPrefs(getActivity());
    View view = inflater.inflate(R.layout.fragment_profile, null);
    TextView userName=view.findViewById(R.id.userName);
    String a=sharedPrefs.getLogedInKey();
    userName.setText(a);

      return view;
    }
    @Override

    public void onViewCreated(@NonNull View view, @Nullable Bundle                  
    savedInstanceState) {
   super.onViewCreated(view, savedInstanceState);

   }}
票数 1
EN

Stack Overflow用户

发布于 2019-06-26 08:32:34

尝试使用

在onCreateView()中

context = getActivity().getApplicationContext();

票数 0
EN

Stack Overflow用户

发布于 2019-06-26 08:36:02

在构造函数中定义sharedpreference:

代码语言:javascript
复制
public ProfileFragment(Context context) {
this.context = context;
sharedPrefs=new SharedPrefs(context);
}

并确保使用密钥LogedInKey保存一些内容

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56763573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档