Travel variable keeps at 1 in the Firebase database, when button is pressed, instead of adding 1 more to the database

Asked

Viewed 34 times

-2

Hello, for some reason the travel variable is not adding 1 more to the Trips field in Firebase, when the pegLoc button is pressed, could you help me? , Follows below code:

inserir a descrição da imagem aqui public class Mainactivity extends Appcompatactivity { private Edittext edtUsuario;

private Button pegLoc;
FirebaseDatabase database;
DatabaseReference mDatabase;
Placa placa;
int viagem = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    pegLoc = findViewById(R.id.btnPegarLoc);
    edtUsuario = findViewById(R.id.edtUsuario);

    placa = new Placa();

    database = FirebaseDatabase.getInstance();
    mDatabase = database.getReference("localização");

    trocarTelas();
}

private void trocarTelas() {

    pegLoc.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (TextUtils.isEmpty(edtUsuario.getText().toString().toUpperCase())) {

                edtUsuario.setError("Insira o Nome do Usuário");
                edtUsuario.requestFocus();

            } else {

                viagem = viagem + 1;

                placa.setPlaca(edtUsuario.getText().toString());

                mDatabase.child("Dados da Viagem").child("loc_Status_Viagem").setValue("Iniciada");

                mDatabase.child("Viagens").child(placa.getPlaca()).setValue(viagem);
                mDatabase.child("Dados da Viagem").child("Placa").setValue(placa.getPlaca());

                mDatabase.child("Dados da Viagem").child("loc_Hora").setValue(AppUtil.getHoraAtual());
                mDatabase.child("Dados da Viagem").child("loc_Data").setValue(AppUtil.getDataAtual());

                Intent intent = new Intent(getApplicationContext(), Localizacao.class);
                startActivity(intent);
            }
        }


     });
    }

}
  • The question n ta mt clear, so tell me one thing, when you click the button, the system manages to fall into Else? to perform the +1?

  • Simm, screen change and all, however, whenever I click to get the location, it keeps at 1, in firebase, instead of adding up 1 more

  • Other data is changed correctly? ous other tbm n changes in firebase?

  • Yes, everyone changes normally, only the one in particular does not change

  • By seeing q to, the problem is q vc has q first update the card, Tries to invert the lines mDatabase.Child("Travel"). Child(plate.getPlaca()). setValue(trip); and mDatabase.Child("Travel Data"). Child("Plate"). setValue(plate.getPlaca());

  • Still the same thing : /

  • Post the structure of your firebase

  • setPlaca(edtUsuario.gettext(). toString()); mDatabase.Child("Travel Data"). Child("loc_Status_Viagem"). setValue("Initiated"); mDatabase.Child("Travels"). Child(plate.getPlaca()). setValue(trip); mDatabase.Child("Trip Data"). Child("Board"). setValue(plate.getPlaca(); mDatabase.Child("Travel Data"). Child("loc_Hora"). setValue(AppUtil.getHoraAtual());
 mDatabase.child("Dados da Viagem"). child("loc_Data"). setValue(AppUtil.getDataAtual());

  • No Paul, asked the question how his firebase bank is structured

  • Ready put

Show 5 more comments

2 answers

0

I was still in doubt about some things, but look at this.

Inside the "Data of the Trip" you have the attribute Board and inside of Trip you an atrubuto with fixed name q would be a plate by q to seeing.

The problem is that the code below updates the card q ta within Travel Data, but the card q has in Travel is fixed.

mDatabase.child("Dados da Viagem").child("Placa").setValue(placa.getPlaca());

That is, if you put the following code, it should work:

mDatabase.child("Viagens").child("hz123").setValue(viagem);
  • It is not fixed, this hz123 is the plate I typed, then the app picks up the typed plate (plaque.getPlaca()) and puts what should be the sum (travel), so the hz123, but even with a fixed field, It also does not sum, in case the line that you analyzed is just to save the board in the trip data, where you will have the location in real time etc, aligns right is mDatabase.Child("Travel"). Child(plate.getPlaca()). setValue(trip);

0

you can use the following method to incremantar 1 automatically by the database.

firebase.database.ServerValue.increment(1)

Browser other questions tagged

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