如何在片段显示共享preference存储的数据? [英] How to display SharedPreference stored data in Fragment?

查看:145
本文介绍了如何在片段显示共享preference存储的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我存储在共享preference值,现在我想访问所有的片段。当我试图运行我的应用程序,它崩溃了。

 公共类证书扩展片段{

按钮提交,更改;
的EditText用户,ID;
上下文CTX;

上下文的背景下= getActivity();

 @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){

     视图窗口= inflater.inflate(R.layout.credential,集装箱,假);
        ((TextView中)windows.findViewById(R.id.textView))的setText(资格证书)。
        提交=((按钮)windows.findViewById(R.id.submit));
        变化=((按钮)windows.findViewById(R.id.change));
        用户=((EditText上)windows.findViewById(R.id.username));
        编号=((EditText上)windows.findViewById(R.id.Useremail));



    。共享preferences preF = getActivity()得到preferences(Context.MODE_PRIVATE);
    字符串username =;
    字符串email =;
    pref.getString(用户名,用户名);
    pref.getString(邮件,邮件);
    Toast.makeText(getActivity(),pressed,Toast.LENGTH_SHORT).show();
    user.setText(用户名);
    id.setText(电子邮件);
}
 

解决方案

我创建了一个的Util类的共享preference,希望它会帮助你。

 进口android.app.Activity;
进口android.content.Context;
进口android.content.Shared preferences;

公共类共享prefrenceUtils {
    公共静态最后弦乐SHARED_ preFERENCE_TAG =TAG_NAME;

    私人共享prefrenceUtils(){
         抛出新的AssertionError();
    }

    公共静态字符串的getString(上下文mContext,字符串键){
        共享preferences preF = mContext.getShared preferences(SHARED_ preFERENCE_TAG,Activity.MODE_PRIVATE);
        返回pref.getString(键,NULL);
    }

    公共静态字符串的getString(上下文mContext,串键,字符串设置defaultValue){
        共享preferences preF = mContext.getShared preferences(SHARED_ preFERENCE_TAG,Activity.MODE_PRIVATE);
        返回pref.getString(键,设置defaultValue);
    }

    公共静态无效putString(上下文mContext,字符串键,字符串值){
        共享preferences preF = mContext.getShared preferences(SHARED_ preFERENCE_TAG,Activity.MODE_PRIVATE);
        共享preferences.Editor编辑器= pref.edit();
        editor.putString(键,值);
        editor.commit();
    }


    公共静态INT调用getInt(上下文mContext,字符串键){
        共享preferences preF = mContext.getShared preferences(SHARED_ preFERENCE_TAG,Activity.MODE_PRIVATE);
        返回pref.getInt(键,0);
    }

    公共静态INT调用getInt(上下文mContext,串键,INT设置defaultValue){
        共享preferences preF = mContext.getShared preferences(SHARED_ preFERENCE_TAG,Activity.MODE_PRIVATE);
        返回pref.getInt(键,设置defaultValue);
    }

    公共静态无效putInt(上下文mContext,串键,int值){
        共享preferences preF = mContext.getShared preferences(SHARED_ preFERENCE_TAG,Activity.MODE_PRIVATE);
        共享preferences.Editor编辑器= pref.edit();
        editor.putInt(键,值);
        editor.commit();
    }

    公共静态长getLong之(上下文mContext,字符串键){
        共享preferences preF = mContext.getShared preferences(SHARED_ preFERENCE_TAG,Activity.MODE_PRIVATE);
        返回pref.getLong(键,0);
    }

    公共静态长getLong之(上下文mContext,串键,长设置defaultValue){
        共享preferences preF = mContext.getShared preferences(SHARED_ preFERENCE_TAG,Activity.MODE_PRIVATE);
        返回pref.getLong(键,设置defaultValue);
    }

    公共静态无效putLong(上下文mContext,串键,长值){
        共享preferences preF = mContext.getShared preferences(SHARED_ preFERENCE_TAG,Activity.MODE_PRIVATE);
        共享preferences.Editor编辑器= pref.edit();
        editor.putLong(键,值);
        editor.commit();
    }


    公共静态布尔getBoolean(上下文mContext,字符串键){
        共享preferences preF = mContext.getShared preferences(SHARED_ preFERENCE_TAG,Activity.MODE_PRIVATE);
        返回pref.getBoolean(键,FALSE);
    }

    公共静态布尔getBoolean(上下文mContext,串键,布尔设置defaultValue){
        共享preferences preF = mContext.getShared preferences(SHARED_ preFERENCE_TAG,Activity.MODE_PRIVATE);
        返回pref.getBoolean(键,设置defaultValue);
    }

    公共静态无效putBoolean(上下文mContext,串键,布尔值){
        共享preferences preF = mContext.getShared preferences(SHARED_ preFERENCE_TAG,Activity.MODE_PRIVATE);
        共享preferences.Editor编辑器= pref.edit();
        editor.putBoolean(键,值);
        editor.commit();
    }

    公共静态无效删除(背景mContext,字符串键){
            共享preferences preF = mContext.getShared preferences(SHARED_ preFERENCE_TAG,Activity.MODE_PRIVATE);
            共享preferences.Editor编辑器= pref.edit();
            editor.remove(键);
            editor.commit();
    }

    公共静态无效清晰(背景mContext){
            共享preferences preF = mContext.getShared preferences(SHARED_ preFERENCE_TAG,Activity.MODE_PRIVATE);
            共享preferences.Editor编辑器= pref.edit();
            editor.clear();
            editor.commit();
    }

}
 

现在存储字符串值:下面的语句中使用您的片段

 共享prefrenceUtils.putString(mContext,用户名,1234);
共享prefrenceUtils.putString(mContext,电子邮件,xyz@gmail.com);
 

要获得字符串值:下面的语句中使用您的片段

 字符串username =共享prefrenceUtils.getString(mContext,用户名);
字符串email =共享prefrenceUtils.getString(mContext,电子邮件);
 

同样的方式,您可以存储和检索应用程序中的任何片段或活动的任何其它的值。

I stored values in SharedPreference and now I want to access all that in Fragment. When I tried to run my App, it crashed.

public class Credentials extends Fragment {

Button submit, change;
EditText user, id;
Context ctx;

Context context = getActivity();

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

     View windows = inflater.inflate(R.layout.credential, container, false);
        ((TextView)windows.findViewById(R.id.textView)).setText("Credentials");
        submit = ((Button)windows.findViewById(R.id.submit));
        change = ((Button)windows.findViewById(R.id.change));
        user = ((EditText)windows.findViewById(R.id.username));
        id = ((EditText)windows.findViewById(R.id.Useremail));



    SharedPreferences pref = getActivity().getPreferences(Context.MODE_PRIVATE);
    String username = "";
    String email = "";
    pref.getString("username", username);
    pref.getString("email", email);
    Toast.makeText(getActivity(), "Pressed", Toast.LENGTH_SHORT).show();
    user.setText(username);
    id.setText(email);
}

解决方案

I have Created a Util Class for SharedPreference, Hope It will help you.

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class SharedPrefrenceUtils {
    public static final String SHARED_PREFERENCE_TAG="TAG_NAME";

    private SharedPrefrenceUtils(){
         throw new AssertionError();
    }

    public static String getString(Context mContext, String key){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getString(key, null);
    }

    public static String getString(Context mContext, String key,  String defaultValue){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getString(key, defaultValue);
    }

    public static void putString(Context mContext, String key, String value ){
        SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(key, value);
        editor.commit();
    }


    public static int getInt(Context mContext, String key){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getInt(key, 0);
    }

    public static int getInt(Context mContext, String key,  int defaultValue){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getInt(key, defaultValue);
    }

    public static void putInt(Context mContext, String key, int value ){
        SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static long getLong(Context mContext, String key){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getLong(key, 0);
    }

    public static long getLong(Context mContext, String key,  long defaultValue){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getLong(key, defaultValue);
    }

    public static void putLong(Context mContext, String key, long value ){
        SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putLong(key, value);
        editor.commit();
    }


    public static boolean getBoolean(Context mContext, String key){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getBoolean(key, false);
    }

    public static boolean getBoolean(Context mContext, String key,  boolean defaultValue){
        SharedPreferences pref = mContext.getSharedPreferences(SHARED_PREFERENCE_TAG,Activity.MODE_PRIVATE);
        return pref.getBoolean(key, defaultValue);
    }

    public static void putBoolean(Context mContext, String key, boolean value ){
        SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static void remove(Context mContext, String key){
            SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.remove(key);
            editor.commit();
    }

    public static void clear(Context mContext){
            SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.clear();
            editor.commit();
    }

}

Now to store String Value: use below statement in your fragment

SharedPrefrenceUtils.putString(mContext,"username", "1234");
SharedPrefrenceUtils.putString(mContext,"email", "xyz@gmail.com");

To get String Value: use below statement in your fragment

String userName = SharedPrefrenceUtils.getString(mContext,"username");
String email = SharedPrefrenceUtils.getString(mContext,"email");

Same way you can store and retrieve any other value from any fragment or activity of your application.

这篇关于如何在片段显示共享preference存储的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆