firebase = new Firebase(Config.FIREBASE_URL_FARE);
firebase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getChildrenCount() == 0) {
Fare fare = new Fare();
fare.setOrdinaryRegFirstFive(ordinaryRegularFF);
fare.setOrdinaryRegSucceeding(ordinaryRegSucceeding);
fare.setOrdinaryDiscountedFirstFive(ordinaryDisFF);
fare.setOrdinaryDiscountedSucceeding(ordinaryDisSucceeding);
fare.setAirconRegPerKm(airconRegPerKm);
fare.setAirconDiscountedPerKm(airconDisPerKm);
fare.setBusCompany(pref.getString("companyName", ""));
firebase.push().setValue(fare);
edtOrdinaryRegFF.setText("");
edtOrdinaryRegSucceeding.setText("");
edtOrdinaryDisFF.setText("");
edtOrdinaryDisSucceeding.setText("");
edtAirconRegPerKm.setText("");
edtAirconDisPerKm.setText("");
Toast.makeText(BusFareActivity.this, "Setting fare successfully.", Toast.LENGTH_LONG).show();
}
else {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
if (postSnapshot.child("busCompany").exists() && postSnapshot.child("busCompany").equals(pref.getString("companyName", ""))) {
postSnapshot.getRef().child("ordinaryRegFirstFive").setValue(ordinaryRegularFF);
postSnapshot.getRef().child("ordinaryRegSucceeding").setValue(ordinaryRegSucceeding);
postSnapshot.getRef().child("ordinaryDiscountedFirstFive").setValue(ordinaryDisFF);
postSnapshot.getRef().child("ordinaryDiscountedSucceeding").setValue(ordinaryDisSucceeding);
postSnapshot.getRef().child("airconRegPerKm").setValue(airconRegPerKm);
postSnapshot.getRef().child("airconDiscountedPerKm").setValue(airconDisPerKm);
Toast.makeText(BusFareActivity.this, "Successfully updated the fare.", Toast.LENGTH_LONG).show();
check++;
}
}
if (check == 0) {
Fare fare = new Fare();
fare.setOrdinaryRegFirstFive(ordinaryRegularFF);
fare.setOrdinaryRegSucceeding(ordinaryRegSucceeding);
fare.setOrdinaryDiscountedFirstFive(ordinaryDisFF);
fare.setOrdinaryDiscountedSucceeding(ordinaryDisSucceeding);
fare.setAirconRegPerKm(airconRegPerKm);
fare.setAirconDiscountedPerKm(airconDisPerKm);
fare.setBusCompany(pref.getString("companyName", ""));
firebase.push().setValue(fare);
edtOrdinaryRegFF.setText("");
edtOrdinaryRegSucceeding.setText("");
edtOrdinaryDisFF.setText("");
edtOrdinaryDisSucceeding.setText("");
edtAirconRegPerKm.setText("");
edtAirconDisPerKm.setText("");
Toast.makeText(BusFareActivity.this, "Setting fare successfully.", Toast.LENGTH_LONG).show();
startActivity(new Intent(BusFareActivity.this, BusFareActivity.class));
}
check = 0;
}
来自Fare.java
的所有数据都是busCompany
以外的两倍。我不明白为什么它无法解析setValue方法的快照。
public class Fare {
private double ordinaryRegFirstFive;
private double ordinaryRegSucceeding;
private double ordinaryDiscountedFirstFive;
private double ordinaryDiscountedSucceeding;
private double airconRegPerKm;
private double airconDiscountedPerKm;
private String busCompany;
public Fare() {}
public void setOrdinaryRegFirstFive(double ordinaryRegFirstFive) {
this.ordinaryRegFirstFive = ordinaryRegFirstFive;
}
public void setOrdinaryRegSucceeding(double ordinaryRegSucceeding) {
this.ordinaryRegSucceeding = ordinaryRegSucceeding;
}
public void setOrdinaryDiscountedFirstFive(double ordinaryDiscountedFirstFive) {
this.ordinaryDiscountedFirstFive = ordinaryDiscountedFirstFive;
}
public void setOrdinaryDiscountedSucceeding(double ordinaryDiscountedSucceeding) {
this.ordinaryDiscountedSucceeding = ordinaryDiscountedSucceeding;
}
public void setAirconRegPerKm(double airconRegPerKm) {
this.airconRegPerKm = airconRegPerKm;
}
public void setAirconDiscountedPerKm(double airconDiscountedPerKm) {
this.airconDiscountedPerKm = airconDiscountedPerKm;
}
public void setBusCompany(String busCompany) {
this.busCompany = busCompany;
}
}
Firebase错误:
com.firebase.client.FirebaseException:无法解析为快照
答案 0 :(得分:1)
您在pojo类中使用私有变量,这可能是异常的原因。 当您尝试将模型类推送到firebase时,变量应该是公共的。