I need to click the button twice to get the value

Asked

Viewed 234 times

0

Someone knows what I did wrong, because I need to double-click the button to pull the value.

package fabiohcnobre.jhotelcolonialdosnobres;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class settingsActivity extends AppCompatActivity {

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef ;

    private String TAG;
    private String statusdareserva;

    private GoogleApiClient client;

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




        Button btnbuscar = (Button) findViewById(R.id.button_buscar);
        btnbuscar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TextView textview1 = (TextView) findViewById(R.id.TextView1);
                EditText editext1 = (EditText) findViewById(R.id.edittext1);
                String status = "Falha";
                status = ProcuraReserva(editext1.getText().toString());
                textview1.setText("1"+ status);
            }
        });
    }


    public String ProcuraReserva(String codigodareserva){
        // Read from the database
        myRef = database.getReference("reserva/"+codigodareserva+"/status");
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String value = dataSnapshot.getValue(String.class);
                statusdareserva = value;
            }

            @Override
            public void onCancelled(DatabaseError error) {
                statusdareserva = "Status com falha";
            }
        });

        return statusdareserva;

    }
}

2 answers

2


The value of statusdareserva is obtained in the method ProcuraReserva() asynchronously.
Notice you had to create a Valueeventlistener to receive it.

That is, the execution comes to the line return statusdareserva; before statusdareserva receive a value in onDataChange() or onCancelled().

Meanwhile the asynchronous process(database.getReference()) ends and statusdareserva receives an amount, which is now available to be returned in return statusdareserva;, when clicking for the second time.

You have to "set" the Textview inside the methods onDataChange() is onCancelled(), because that’s where it’s available when it’s read from the database.

public void ProcuraReserva(String codigodareserva){
    // Read from the database
    myRef = database.getReference("reserva/"+codigodareserva+"/status");
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String value = dataSnapshot.getValue(String.class);
            textview1.setText("1"+ value);
        }

        @Override
        public void onCancelled(DatabaseError error) {
            textview1.setText("Status com falha");
        }
    });
}

You’ll have to declare textview1 as an attribute of Activity and initiate it in the method onCreate()

  • 1

    Thanks for the help Ramaral gave to understand right how the code works.

1

In this section, the process is entering the method, and before finishing textview1.settext("1"+ status); it has already been activated.

status = ProcuraReserva(editext1.getText().toString());
            textview1.setText("1"+ status);

Tries to place the textview as a global variable and make the change within the Search method();

test also if you are entering the ways onDataChange and onCancelled, I’m not sure.

  • Thanks for the help Icaro.

Browser other questions tagged

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