リストビューで古い結果を表示する履歴アクティビティを持つ電卓を正常に実装しましたが、最初の行が常にNULLであるという問題に直面しています計算結果を保存し、SharedPreferencesを使用してリストとして表示
私は、デフォルトの文字列は、「オールド・計算」
> String w = "Old Calculation";
作ってみましたが、これは私のアプリが複数行Sに結果を保存し 、動作しませんでしたonCreate下SharedPreferencesにトリングと意図
>SharedPreferences.Editor editor ; // saving shared preferences editor as MainAcitivty class attribute
介して別のアクティビティに渡す私は文字列sの結果の文字列値を更新する
> SharedPreferences prefs = getPreferences(MODE_PRIVATE);
w = prefs.getString("saved", null);
更新方法をsharedprefする文字列値を設定します(文字列sが計算結果である)私はのTextViewリストに結果が表示されたアクティビティにWの値を渡す
> 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();
}
> 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);
}
歴史活動
アクティビティ からのデータを保存し、別のアクティビティから、それを取得したい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();
}
}
}