Receive parameters of different activity and add

0

Hello,

I need to pass the two-screen parameters to the home screen , or the "BALANCE" parameters of the balance screen where it will be passed to an EditText (lblStandard) start screen and then take a value set to "EXTRA" on the addarsaldo screen and add to the "BALANCE" parameter and show in the ActualScreen on the home screen screen >

Help please Thank you.

SCREENSHOW screenshot

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

ADDOSAL TAB

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

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);
    }
}
    
asked by anonymous 25.10.2016 / 21:42

1 answer

2

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

What value is being shown to you in textView lblStartActual ?

Are you moving to the Intent of call of this Activity values correctly? For example:

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

I was in doubt as to the condition:

if (lblSaldoAtual != null) {

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

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

Some addenda:

Is all this code being called within onCreate ?

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

---------------- New Reply --------------------

When you use Serializable while moving from screen to screen, we need to pass all values . What is happening is that the "BALANCE" is being zeroed after the call of acrescentarsaldo .

You will need to include in the% of% of calls to onClick the amount of the Balance:

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

before acrescentarsaldo

One note, add the command startActivity(it); to prevent them from getting many open activities.

You will need to enter in finish(); :

private String saldo;

within acrescentarsaldo :

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

and return to onCreate :

it.putExtra("SALDO", saldo);

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

I hope I have helped!

    
26.10.2016 / 16:10