Receive parameters of different Actions and add

Asked

Viewed 348 times

0

Hello,

I need to pass the parameters of two screens to the tela_inicio, be the "BALANCE" parameters of the screen balance where it will be passed to an Edittext (lblSaldoAtual) at tela_inicio and then pick up a value put in "EXTRA" on the screen addirsald and add to the "BALANCE" parameter and display in lblSaldoAtual on the screen tela_inicio

Help please Thank you.

START SCREEN

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tela_inicio);

    lblSaldoAtual = (TextView) findViewById(R.id.lblSaldoAtual);
    btnSaldo = (Button) findViewById(R.id.btnSaldo);
    lblSaldoTotal = (TextView) findViewById(R.id.lblSaldoTotal);
    btnMenos = (ImageView) findViewById(R.id.btnMenos);
    bMais = (Button) findViewById(R.id.bMais);
    bMenos = (Button) findViewById(R.id.bMenos);

    btnSaldo.setOnClickListener(this);
    btnMenos.setOnClickListener(this);
    bMais.setOnClickListener(this);
    bMenos.setOnClickListener(this);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    Classe1 classe = new Classe1();
    classe.recebesaldo();
    classe.recebeextra();
}

class Classe1 {
    Double extra, saldoatual, saldototal, total;
    public void recebesaldo() {
        Intent it = getIntent();
        if (it != null) {
            Bundle b1 = it.getExtras();
                saldoatual = Double.parseDouble(b1.getString("SALDO"));
                lblSaldoAtual.setText(Double.toString(saldoatual));
                saldototal = Double.parseDouble(b1.getString("SALDO"));
                lblSaldoTotal.setText(Double.toString(saldototal));

            }
        }
   public void recebeextra() {
                Intent it2 = getIntent();
                extra = Double.parseDouble(it2.getStringExtra("EXTRA"));
                total = extra + saldoatual;
                lblSaldoAtual.setText(Double.toString(total));
   }
}
public void onClick(View view) {
    if (view == btnSaldo) {
        Intent it = new Intent(this, saldo.class);
        startActivity(it);}
      else if (view == bMais) {
        Intent it = new Intent(this, acrescentarsaldo.class);
        startActivity(it);
    } else if (view == bMenos) {
        Intent it = new Intent(this, acrescentarsaldo.class);
        startActivity(it);
    }
}

SPLASH SCREEN

public class acrescentarsaldo extends AppCompatActivity implements View.OnClickListener{
    private Button btnOK;
    private EditText txtAcrescentarSaldo;
    public static String acrescenta;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acrescentarsaldo);
        btnOK = (Button) findViewById(R.id.btnOK);
        txtAcrescentarSaldo = (EditText) findViewById(R.id.txtAcrescentarSaldo);

        btnOK.setOnClickListener(acrescentarsaldo.this);

    }
    public static String getAcrescenta(){
        return acrescenta;

    }
    public void onClick(View v) {
        Intent it = new Intent(acrescentarsaldo.this, tela_inicio.class);
        it.putExtra("EXTRA", txtAcrescentarSaldo.getText().toString());
        startActivity(it);
    }
}

SCREEN BALANCE

public class saldo extends AppCompatActivity implements View.OnClickListener{
    private EditText txtAtualizar;
    private Button btnAtualizar;
    private static String saldo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saldo);
        btnAtualizar = (Button) findViewById(R.id.btnAtualizar);
        txtAtualizar = (EditText) findViewById(R.id.txtAtualizar);

        btnAtualizar.setOnClickListener(saldo.this);
    }
    public static String getSaldo(){
        return saldo;
    }
    @Override
    public void onClick(View view) {
        Intent it = new Intent(saldo.this, tela_inicio.class);
        it.putExtra("SALDO", txtAtualizar.getText().toString());
        startActivity(it);
    }
}
  • See if it’s this what you seek.

  • No, I couldn’t use it this way

  • You’ll have to explain better because it’s not clear what you want.

  • I edited the question, I hope I was clearer

1 answer

2


Only with this code is it hard to understand what really "is not working".

What value is being displayed to you on textView lblSaldoAtual?

Is passing to the Intent call this Activity the values correctly? For example:

intent.putExtra("SALDO", "5000");
intent.putExtra("EXTRA", "200");

I was in doubt on condition:

if (lblSaldoAtual != null) {

Is the code running in loop? the first time empty and the second filled?

I recommend you insert Logs into the code, or run in the debug mode to find where it is falling. And identify if the variables being declared at the beginning of the Classe1 (Double extra, saldoatual, saldototal, total;) are being initialized when creating a new object with the line Classe1 classe = new Classe1(); ?

Some addendums:

All this code is being called inside the onCreate?

Avoid declaring a variable with the same name twice, equal to Double extra.

----------------New Answer--------------------

When using the Serializable when transiting from screen to screen, we need to pass all values. What is occurring is that the "BALANCE" is being reset after the call of the acrescentarsaldo.

You will need to include in onClick of calls to acrescentarsaldo the value of the Balance:

it.putExtra("SALDO", lblSaldoAtual.getText().toString());

before the startActivity(it);

One remark, add command finish(); to prevent them from remaining with numerous open activities.

You will need to enter in acrescentarsaldo:

private String saldo;

inside onCreate:

saldo = getIntent().getStringExtra("SALDO");

and on return to tela_inicial:

it.putExtra("SALDO", saldo);

Just to finish, use try catches in the parts of the code where you convert String for Double, to prevent the application from breaking.

I hope I’ve helped!

  • I edited the question, I hope I was clearer. As for the if (lblSaldoAtual != null) { I was running tests and I just missed it. And when I run by debug, the impression I get is that it is running both Intent at the same time

  • I edited my answer, you can start from New Answer. Att

  • Another question, what would be the right method to pass all these variables to Double? The form I used is in agreement?

  • The method is correct, yes! A nice thing to do is to turn a comma into points, thus staying: Double.parseDouble(var.replace(",", ".")) . It is never known what the user will type. You can also remove spaces to make sure that the transformation of the String do not "break" the application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.