Java 保存计算器结果并使用SharedReferences将其显示为列表

Java 保存计算器结果并使用SharedReferences将其显示为列表,java,android,sharedpreferences,calculator,history,Java,Android,Sharedpreferences,Calculator,History,我已经成功地实现了一个带有历史记录活动的计算器,其中它将在列表视图中显示旧的结果 但是我面临一个问题,第一行总是空的 我尝试将默认字符串设置为“旧计算” 但这不管用, 我的应用程序将结果保存在SharedReferences中的多行字符串中,并通过intent将其传递给另一个活动 >SharedPreferences.Editor editor ; // saving shared preferences editor as MainAcitivty class attribute 在

我已经成功地实现了一个带有历史记录活动的计算器,其中它将在列表视图中显示旧的结果 但是我面临一个问题,第一行总是空的

我尝试将默认字符串设置为“旧计算”

但这不管用, 我的应用程序将结果保存在SharedReferences中的多行字符串中,并通过intent将其传递给另一个活动

>SharedPreferences.Editor editor ; // saving shared preferences editor as MainAcitivty class attribute
在onCreate下,我将字符串值设置为sharedpref

> SharedPreferences prefs = getPreferences(MODE_PRIVATE);
        w = prefs.getString("saved", null);
更新方法,其中我使用字符串s更新结果字符串值(字符串s是计算结果)

将W值传递给活动,我将在其中在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);
    }
历史活动

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

当您存储活动A中的数据并希望从另一个活动中检索数据时,必须使用
SharedReferences
,如下所示:

SharedReferences prefs=getSharedReferences(字符串,int)

String
是类似于“result”的文件名
int
的模式类似于私有模式
例如见

此外,我还需要你的帮助

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