【问题标题】:How to display SharedPreference stored data in Fragment?如何在 Fragment 中显示 SharedPreference 存储的数据?
【发布时间】:2015-03-11 12:15:59
【问题描述】:

我将值存储在 SharedPreference 中,现在我想访问 Fragment 中的所有内容。当我尝试运行我的应用程序时,它崩溃了。

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);
}

【问题讨论】:

  • 你的 logcat 说了什么???贴出来
  • 感谢快速回复...我怎样才能获得...价值我 textView 请给我提示
  • 你忘了返回一个值或者它不是整个方法? return windows
  • 是的,我添加了返回的窗口...
  • 我将用户名和密码存储在 SharedPreference 中,现在我想获取另一个片段,我想获取该用户名和注册的电子邮件 ID

标签: android android-intent android-activity


【解决方案1】:

我已经为 SharedPreference 创建了一个 Util 类,希望对您有所帮助。

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();
    }

}

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

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

要获取字符串值:在您的片段中使用以下语句

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

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

【讨论】:

  • 谢谢大家...我解决了我的问题...再次感谢大家...
  • 为什么要在每个函数中添加SharedPreferences pref= mContext.getSharedPreferences(SHARED_PREFERENCE_TAG, Activity.MODE_PRIVATE);。你可以在全球声明。
  • @reegan29 那么静态util方法就没有意义了。
【解决方案2】:

你的接收方式有问题。

public abstract String getString(String key, String defValue)

从首选项中检索字符串值。

参数

key:要检索的首选项的名称。
defValue:如果此首选项不存在,则返回的值。

退货

如果存在,则返回首选项值或 defValue。投掷 ClassCastException 如果此名称的首选项不是 一个字符串。

所以当你使用pref.getString() 方法时,你必须将返回值存储到某个东西。

试试下面的例子。

String username = pref.getString("username", "EMPTY");
Toast.makeText(getActivity(), "Value : "+username, Toast.LENGTH_SHORT).show();

【讨论】:

    猜你喜欢
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    相关资源
    最近更新 更多