保存计算器结果并使用 SharedPreferences 将它们显示为列表
save calculator result and show them as a list using SharedPreferences
我已经成功实现了一个具有历史记录的计算器 activity,它将在列表视图中显示旧结果
但我遇到的问题是第一行总是空的
我尝试制作默认字符串“旧计算”
> String w = "Old Calculation";
但这没有用,
我的应用程序将结果保存在 SharedPreferences 中的多行字符串中,并通过 intent
将其传递给另一个 activity
>SharedPreferences.Editor editor ; // saving shared preferences editor as MainAcitivty class attribute
在 onCreate 下,我将字符串值设置为 sharedpref
> SharedPreferences prefs = getPreferences(MODE_PRIVATE);
w = prefs.getString("saved", null);
update 方法,其中我用 String s 更新结果字符串值(String s 是计算结果)
> public void update(String s){
editor= getPreferences(MODE_PRIVATE).edit();
w = w
+ System.getProperty ("line.separator")
+ s
+ System.getProperty ("line.separator");
editor.putString("saved", w);
editor.apply();
}
将 W 值传递给 activity,其中我将在 TextView 列表中显示结果
> public void History(View v){
Intent myIntent = new Intent(MainActivity.this, History.class);
myIntent.putExtra("message", w);
startActivity(myIntent);
overridePendingTransition(R.anim.anim_slide_in_left,
R.anim.anim_slide_out_left);
}
历史activity
public class History extends AppCompatActivity {
TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
String w ="Old calculation is showed here";
tv1 = (TextView) findViewById(R.id.tv1);
print(w);
}
public void print(String w) {
Bundle bundle = getIntent().getExtras();
w = bundle.getString("message");
tv1.setMovementMethod(new ScrollingMovementMethod());
tv1.setText(w);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_right);
}
public void deleteAppData(View v) {
try {
// clearing app data
String packageName = getApplicationContext().getPackageName();
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear "+packageName);
} catch (Exception e) {
e.printStackTrace();
}
}
}
当您存储来自 activity A 的数据并希望从另一个 activity 检索数据时,您必须使用 SharedPreferences
,如下所示:
SharedPreferences prefs = getSharedPreferences(String, int);
String
是类似于 "result"
的文件名
int
的模式类似于 MODE_PRIVATE
例如参见 [=14=]
另外我wrote a simple code帮你
我已经成功实现了一个具有历史记录的计算器 activity,它将在列表视图中显示旧结果 但我遇到的问题是第一行总是空的
我尝试制作默认字符串“旧计算”
> String w = "Old Calculation";
但这没有用, 我的应用程序将结果保存在 SharedPreferences 中的多行字符串中,并通过 intent
将其传递给另一个 activity>SharedPreferences.Editor editor ; // saving shared preferences editor as MainAcitivty class attribute
在 onCreate 下,我将字符串值设置为 sharedpref
> SharedPreferences prefs = getPreferences(MODE_PRIVATE);
w = prefs.getString("saved", null);
update 方法,其中我用 String s 更新结果字符串值(String s 是计算结果)
> public void update(String s){
editor= getPreferences(MODE_PRIVATE).edit();
w = w
+ System.getProperty ("line.separator")
+ s
+ System.getProperty ("line.separator");
editor.putString("saved", w);
editor.apply();
}
将 W 值传递给 activity,其中我将在 TextView 列表中显示结果
> public void History(View v){
Intent myIntent = new Intent(MainActivity.this, History.class);
myIntent.putExtra("message", w);
startActivity(myIntent);
overridePendingTransition(R.anim.anim_slide_in_left,
R.anim.anim_slide_out_left);
}
历史activity
public class History extends AppCompatActivity {
TextView tv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
String w ="Old calculation is showed here";
tv1 = (TextView) findViewById(R.id.tv1);
print(w);
}
public void print(String w) {
Bundle bundle = getIntent().getExtras();
w = bundle.getString("message");
tv1.setMovementMethod(new ScrollingMovementMethod());
tv1.setText(w);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_right);
}
public void deleteAppData(View v) {
try {
// clearing app data
String packageName = getApplicationContext().getPackageName();
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear "+packageName);
} catch (Exception e) {
e.printStackTrace();
}
}
}
当您存储来自 activity A 的数据并希望从另一个 activity 检索数据时,您必须使用 SharedPreferences
,如下所示:
SharedPreferences prefs = getSharedPreferences(String, int);
String
是类似于 "result"
的文件名
int
的模式类似于 MODE_PRIVATE
例如参见 [=14=]
另外我wrote a simple code帮你