I need to click the button twice to get the value

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;

    }
}
    
asked by anonymous 15.09.2016 / 04:43

2 answers

2

The statusdareserva value is obtained in the ProcuraReserva() method asynchronously. Note that you had to create a ValueEventListener to receive it.

That is, the execution arrives at the return statusdareserva; line before statusdareserva receives a value in onDataChange() or onCancelled() .

However the asynchronous process ( database.getReference() ) ends and statusdareserva receives a value, which is now available to be returned in return statusdareserva; , when clicking a second time.

You have to "set" the TextView within the methods onDataChange() is onCancelled() , since that is where it is available when it is 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 must declare textview1 as an attribute of Activity and start it in method onCreate()

    
15.09.2016 / 12:12
1

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

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

Try to put the textview a global variable and make the change within the SearchStatus () method;

Test also if you are entering the onDataChange and onCancelled methods, I'm not sure.

    
15.09.2016 / 06:23