Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何保存值和键等数据?_Android_Sharedpreferences - Fatal编程技术网

Android 如何保存值和键等数据?

Android 如何保存值和键等数据?,android,sharedpreferences,Android,Sharedpreferences,我想建立一个应用程序来报告收入和费用。 我想保存一些数据,比如有多少钱进来,什么时候进来,怎么进来。 并构建结构化类别,如1可乐=-1.5美元等。当我关闭应用程序并重新启动手机时,数据仍然保存。我只是一个初学者,所以我被困在了保存数据的步骤中 现在我要做的是使用SharedReferences来保存预算,然后在TextView中打印出来。而且它不起作用 我的活动: public class Report extends AppCompatActivity { Button mButton1;

我想建立一个应用程序来报告收入和费用。 我想保存一些数据,比如有多少钱进来,什么时候进来,怎么进来。 并构建结构化类别,如1可乐=-1.5美元等。当我关闭应用程序并重新启动手机时,数据仍然保存。我只是一个初学者,所以我被困在了保存数据的步骤中

现在我要做的是使用SharedReferences来保存预算,然后在TextView中打印出来。而且它不起作用

我的活动:

public class Report extends AppCompatActivity {

Button mButton1;
EditText mEdit;
TextView mText;
Button mButton;

SharedPreferences menaPref;
SharedPreferences.Editor editor;


public static final String MY_PREFS_NAME = "My Money";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_report);

    mButton = (Button) findViewById(R.id.B_GetMoney);
    mEdit = (EditText) findViewById(R.id.T_InputMoney);
    mText = (TextView) findViewById(R.id.textView);
    mButton1 = (Button) findViewById(R.id.B_try);


    mButton.setOnClickListener(


            new View.OnClickListener() {
                public void onClick(View view) {
                    String value = mEdit.getText().toString();
                    ;


                    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
                    SharedPreferences.Editor editor = pref.edit();

                    editor.putString("My Money", value);
                    String Show_My_Money = pref.getString("My Money", null);

                    mText.setText(Show_My_Money);


                }

            });

}

}
您需要
.commit()
.apply()
在编辑器中放入内容后,首先对其进行更改

editor.putString("My Money", value);
editor.commit()
pref.getString("My Money", null);
您需要
.commit()
.apply()
在编辑器中放入内容后,首先对其进行更改

editor.putString("My Money", value);
editor.commit()
pref.getString("My Money", null);
必须使用commit()或apply()保存数据;)

请参阅本文档:

必须使用commit()或apply()保存数据;)

请参阅本文档:


创建一个类预处理帮助程序

public class PrefHelper{

private static final String PREF_NAME = "my_pref";

public static final String MY_MONEY= "my_money";
// put your others key here


Preferences() {
}

public static void writeBoolean(Context context, String key, boolean value) {
    getEditor(context).putBoolean(key, value).commit();

}

public static boolean readBoolean(Context context, String key) {
    return getPreferences(context).getBoolean(key, false);
}

public static void writeString(Context context, String key, String value) {
    getEditor(context).putString(key, value).commit();
}

public static String readString(Context context, String key) {
    return getPreferences(context).getString(key, null);
}


private static SharedPreferences getPreferences(Context context) {
    return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}

private static SharedPreferences.Editor getEditor(Context context) {
    return getPreferences(context).edit();
}
}
对于读写,请使用这些方法

// for write data
PrefHelper.writeString(getApplicationContext(), PrefHelper.MY_MONEY, "500 $");

// for read data
String money = PrefHelper.readString(getApplicationContext(), PrefHelper.MY_MONEY);

创建一个类PrefHelper

public class PrefHelper{

private static final String PREF_NAME = "my_pref";

public static final String MY_MONEY= "my_money";
// put your others key here


Preferences() {
}

public static void writeBoolean(Context context, String key, boolean value) {
    getEditor(context).putBoolean(key, value).commit();

}

public static boolean readBoolean(Context context, String key) {
    return getPreferences(context).getBoolean(key, false);
}

public static void writeString(Context context, String key, String value) {
    getEditor(context).putString(key, value).commit();
}

public static String readString(Context context, String key) {
    return getPreferences(context).getString(key, null);
}


private static SharedPreferences getPreferences(Context context) {
    return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}

private static SharedPreferences.Editor getEditor(Context context) {
    return getPreferences(context).edit();
}
}
对于读写,请使用这些方法

// for write data
PrefHelper.writeString(getApplicationContext(), PrefHelper.MY_MONEY, "500 $");

// for read data
String money = PrefHelper.readString(getApplicationContext(), PrefHelper.MY_MONEY);

thanx,你知道我怎样才能生成报告,比如:时间:凌晨1:30类别:食物thanx,你知道我怎样才能生成报告,比如:时间:凌晨1:30类别:食物