我从firebase数据库中获取数据时出现问题。
具体问题是,onDataChange
方法(ValueEventlistener
)未被调用。
申请继续:
DatabaseConnectionManager()
readFromDatabase()
getEventFormulaGet()
eventFormulaGet
为空,因为onDataChange
未被调用....
代码:
package bbcag.ch.miivent_luca;
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 DatabaseConnectionManager implements ValueEventListener {
private FirebaseDatabase database;
private DatabaseReference databaseReference;
private EventFormula eventFormulaSend;
private EventFormula eventFormulaGet;
public DatabaseConnectionManager() {
createConnectionToDatabase();
}
public void createConnectionToDatabase() {
database = FirebaseDatabase.getInstance();
databaseReference = database.getReference();
}
public void readFromDatabase() {
databaseReference.child("events").addListenerForSingleValueEvent(this);
}
public void deleteFromDatabase() {
}
public void setEventFormula(EventFormula eventFormula) {
this.eventFormulaSend = eventFormula;
}
public void sendFormulaToDatabase() {
databaseReference.child("events").child(eventFormulaSend.getEventTitle()).setValue(eventFormulaSend);
}
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
eventFormulaGet = dataSnapshot.getValue(EventFormula.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
public EventFormula getEventFormulaGet() {
return eventFormulaGet;
}
}