英文:
Firebase OnDataChange not transitioning to next activity
问题
I have some problems. I want to retrieve the value of the state for order. When the Customer creates an order, the value will be added to Firebase as "Pending". After that, the customer will wait until the Driver accepts the order, then the value will change from "Pending" to "Accepted".
public class C_Offers extends AppCompatActivity {
String OrderID;
String phoneNO;
String State = null;
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c_offers);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
phoneNO = bundle.getString("Phone");
OrderID = bundle.getString("OrderID");
}
databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child("Customer").child(phoneNO).child("Order").child(OrderID);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
State = dataSnapshot.child("State").getValue(String.class);
Toast.makeText(C_Offers.this, "State : " + State, Toast.LENGTH_SHORT).show();
if (State.equals("Accepted")) {
startActivity(new Intent(C_Offers.this, C_Order.class));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
When using this:
Toast.makeText(C_Offers.this, "State : " + State, Toast.LENGTH_SHORT).show();
It will print "Pending". After changing the value on the Driver's side, it will change and print "Accepted".
The exact problem is inside this:
if (State.equals("Accepted")) {
startActivity(new Intent(C_Offers.this, C_Order.class));
}
If the state is not being read properly and the application is closing, go back to the previous layout. You need to check if the value in Firebase has changed to "Accepted" or not. If it has changed, then proceed to a new activity. This is what you want.
英文:
I have some problems. I want to retrieve the value of the state for order, first, when the Customer creates order the value will add to the firebase "Pending" after that, the customer will wait until Driver accept the order then will change the value from "Pending" to "Accepted".
public class C_Offers extends AppCompatActivity {
String OrderID;
String phoneNO;
String State = null;
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c_offers);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
phoneNO = bundle.getString("Phone");
OrderID = bundle.getString("OrderID");
}
databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child("Customer").child(phoneNO).child("Order").child(OrderID);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
State = dataSnapshot.child("State").getValue(String.class);
Toast.makeText(C_Offers.this, "State : "+State, Toast.LENGTH_SHORT).show();
if (State.equals("Accepted")) {
startActivity(new Intent(C_Offers.this, C_Order.class));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
when use this
Toast.makeText(C_Offers.this, "State : "+State, Toast.LENGTH_SHORT).show();
will print "Pending", after change the value in Driver side will change and printed "Accepted"
The exact problem is inside this
if (State.equals("Accepted")) {
startActivity(new Intent(C_Offers.this, C_Order.class));
}
First not read and close the application, go to back layout
I need to check if value of Firebase is changed to "Accepted" or not
if changed go to new activity this what I want
专注分享java语言的经验与见解,让所有开发者获益!
评论