在 Second Activity 上发布编写 Textview

Issue writing Textview on Second Activity

首先,我很抱歉我的英语不好,我希望我能很好地表达自己。 我有一个应用程序,主要 activity 我有 2 个数字我想发送到第二个 activity 并将它们存储在那里作为这些数字的历史记录,当我关闭应用程序时我不需要它们不再,所以它只是暂时使用。

在这个空隙中,我将数字发送给第二个 class。

public void PasarHistorial() {
    ContadorH++;

    Intent myIntent = new Intent(MainActivity.this,   Historial.class);
    myIntent.putExtra("Res1", counter1);
    myIntent.putExtra("Res2", counter2);
    myIntent.putExtra("ContH",ContadorH);
    startActivity(myIntent);


}

第二个 Class(历史):

public class Historial extends AppCompatActivity {
public  static TextView H1, H2, H3, H4, H5, H6, H7, H8, H9;
public int Resultado1,Resultado2,contador;
public Button Regresar;


public void Regresar(){
    Regresar= (Button)findViewById(R.id.Regresar);
    Regresar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });
}





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_historial);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);



    H1  = (TextView) findViewById(R.id.Historia1);
    H2  = (TextView) findViewById(R.id.Historia2);
    H3  = (TextView) findViewById(R.id.Historia3);
    H4  = (TextView) findViewById(R.id.Historia4);
    H5  = (TextView) findViewById(R.id.Historia5);
    H6  = (TextView) findViewById(R.id.Historia6);
    H7  = (TextView) findViewById(R.id.Historia7);
    H8  = (TextView) findViewById(R.id.Historia8);
    H9  = (TextView) findViewById(R.id.Historia9);
    Regresar();

    int Res1 = getIntent().getIntExtra("Res1",0);
    int Res2 = getIntent().getIntExtra("Res2",0);
    int ContH = getIntent().getIntExtra("ContH",0);
    contador = ContH;
    Resultado1 = Res1;
    Resultado2 = Res2;

    GuardarResultado();

}





public void GuardarResultado() {

                switch (contador) {

                    case 0:
                    break;


       case 1:
          H1.setText("Resultado :" + Resultado1 + " : " + Resultado2);
          break;


       case 2:
           H2.setText("Resultado :" + Resultado1 + " : " + Resultado2);
           break;
       case 3:
           H3.setText("Resultado :" + Resultado1 + " : " + Resultado2);

           break;
       case 4:
           H4.setText("Resultado :" + Resultado1 + " : " + Resultado2);

           break;
       case 5:
           H5.setText("Resultado :" + Resultado1 + " : " + Resultado2);

           break;
       case 6:
           H6.setText("Resultado :" + Resultado1 + " : " + Resultado2);

           break;
       case 7:
           H7.setText("Resultado :" + Resultado1 + " : " + Resultado2);

           break;
       case 8:
           H8.setText("Resultado :" + Resultado1 + " : " + Resultado2);

           break;
       case 9:
           H9.setText("Resultado :" + Resultado1 + " : " + Resultado2);

           break;


                }


            }

}

我是这样做的,因为当我以不同的方式调用它们时,我的文本视图为空。 现在,问题是当我调用函数 1 次时,它会在我需要的地方写入我需要的数字,但再次调用它时,会删除我的第一个结果并写入第二个。 甚至当我回到主 class 并再次回到历史 Class 时,你也会失去一切。 新编程,谢谢大家

如果您想保留号码,可以使用共享首选项。

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("yourint", yourIntValue);
editor.commit();