2016-11-15 5 views
1

私は2つのアクティビティで簡​​単なAndroidアプリを作成しています。アクティビティBには、Aから渡された一連のデータが表示されます。Bのデータを複製しないために、BIからバックボタンを押して変数をクリアする必要がありますが、onResume()まったく解消する。ここに私のコード:onResumeで特定のアクティビティ変数をクリアする

public class MainActivity extends AppCompatActivity { 

List<Camarera> camareras = new ArrayList<Camarera>(); 
List<String> resultados = new ArrayList<String>(); 

int camHabs = 0; 

Button boton; 

TextView dinero; 
TextView camarera1, camarera2, camarera3, camarera4, camarera5; 

EditText hora1, hora2, hora3, hora4, hora5; 
EditText resta1, resta2, resta3, resta4, resta5; 

Switch switch1, switch2, switch3, switch4, switch5; 

@Override 
public void onResume() 
{ 
    super.onResume(); 
    resultados = new ArrayList<>(); 
    camHabs = 0; 
    Toast.makeText(getApplicationContext(),"Resumiendo", Toast.LENGTH_LONG).show(); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    boton = (Button) findViewById(R.id.Boton); 

    camHabs = 0; 

    boton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Calcular(); 
     } 
    }); 
} 

void Calcular(){ 

    dinero = (TextView) findViewById(R.id.Dinero); 

    camarera1 = (TextView) findViewById(R.id.Camarera1); 
    camarera2 = (TextView) findViewById(R.id.Camarera2); 
    camarera3 = (TextView) findViewById(R.id.Camarera3); 
    camarera4 = (TextView) findViewById(R.id.Camarera4); 
    camarera5 = (TextView) findViewById(R.id.Camarera5); 

    hora1 = (EditText) findViewById(R.id.C_Horas1); 
    hora2 = (EditText) findViewById(R.id.C_Horas2); 
    hora3 = (EditText) findViewById(R.id.C_Horas3); 
    hora4 = (EditText) findViewById(R.id.C_Horas4); 
    hora5 = (EditText) findViewById(R.id.C_Horas5); 

    resta1 = (EditText) findViewById(R.id.C_Resta1); 
    resta2 = (EditText) findViewById(R.id.C_Resta2); 
    resta3 = (EditText) findViewById(R.id.C_Resta3); 
    resta4 = (EditText) findViewById(R.id.C_Resta4); 
    resta5 = (EditText) findViewById(R.id.C_Resta5); 

    switch1 = (Switch) findViewById(R.id.C_Switch1); 
    switch2 = (Switch) findViewById(R.id.C_Switch2); 
    switch3 = (Switch) findViewById(R.id.C_Switch3); 
    switch4 = (Switch) findViewById(R.id.C_Switch4); 
    switch5 = (Switch) findViewById(R.id.C_Switch5); 


    List<TextView> nombres = Arrays.asList(camarera1, camarera2, camarera3, camarera4, camarera5); 
    List<EditText> horas = Arrays.asList(hora1, hora2, hora3, hora4, hora5); 
    List<EditText> restas = Arrays.asList(resta1, resta2, resta3, resta4, resta5); 
    List switches = Arrays.asList(switch1, switch2, switch3, switch4, switch5); 

    int resultado; 
    int maxHoras = 0; 
    int Dinero = Integer.parseInt(dinero.getText().toString()); 

    for (int i = 0; i < nombres.size(); i++) { 
     Camarera cam = new Camarera(); 

     cam.SetNombre(nombres.get(i).getText().toString()); 
     cam.SetHoras(horas.get(i).getText().toString()); 
     maxHoras += cam.GetHoras(); 
     cam.SetResta(restas.get(i).getText().toString()); 
     Switch var = (Switch) switches.get(i); 
     cam.activa = var.isChecked(); 

     camareras.add(cam); 
     } 

     for (int i = 0; i < camareras.size(); i++) { 
      Camarera cam = camareras.get(i); 

      if (cam.activa) { 
       camHabs++; 
       if (cam.error) { 
        Toast.makeText(getApplicationContext(),cam.error_S, Toast.LENGTH_LONG).show(); 
        return; 
       } 


       resultado = ((cam.GetHoras() * Dinero)/maxHoras) - cam.GetResta(); 
       resultados.add(cam.GetNombre() + " se lleva " + resultado + "€"); 
      } 
    } 

    Intent intent = new Intent(MainActivity.this, Tabla.class); 
    intent.putExtra("lista", (ArrayList<String>) resultados); 
    intent.putExtra("habs", camHabs); 
    startActivity(intent); 



} 

私を助けてもらえますか?一日中このことに固執した。

答えて

0

Resumeでそれらをnullに等しくしてから、すでに完了したように空の値を再作成してください。それが動作するかどうか私に教えてください。 OnResume上のドキュメントから

0

は、システムは、それが初めて作成だときなど、この方法であなたのアクティビティがフォアグラウンドに入ってくるたびに呼び出すことに注意してください。

ホームボタンを押す/アプリケーションを切り替えるときにも呼び出されるので、ここでこれを行うことをお勧めします。

ちょうどあなたがあなたのstartActivityコールを呼び出す前に、それをしない理由:

Intent intent = new Intent(MainActivity.this, Tabla.class); 
intent.putExtra("lista", (ArrayList<String>) resultados); 
intent.putExtra("habs", camHabs); 

//Reset variables here 
resultados = new ArrayList<>(); 
camHabs = 0; 

startActivity(intent); 
0

を実際に、エラーがこれらの二つの変数にはなかった、それは「camareras」私はクリアしていなかったと呼ばれる第3の1にあったがそれは私が各サイクルでそれを使うので、何度も "camHabs"に追加されます。

ありがとうございました!

関連する問題