我正在写一份申请,与家人分享燃料停止。 但是,我是firebase的新手。在这个例子中,我创建了类Eintrag(Entry)
public class Eintrag {
String name;
double kilometerstand;
double liter;
double preis;
String datum;
public Eintrag()
{}
public Eintrag(String name, double kilometerstand, double liter, double preis, String datum) {
this.name = name;
this.kilometerstand = kilometerstand;
this.liter = liter;
this.preis = preis;
this.datum = datum;
}
public static void neuerEintrag(int index, String name, double kilometerstand, double liter, double preis, String datum) {
MainActivity.log.add(index, new Eintrag(name, kilometerstand, liter, preis, datum));
}
public static void neuerEintrag(String name, double kilometerstand, double liter, double preis, String datum) {
int index = MainActivity.log.size();
MainActivity.log.add(index, new Eintrag(name, kilometerstand, liter, preis, datum));
}
// FIREBASE
public String getName() {
return name;
}
public double getKilometerstand() {
return kilometerstand;
}
public double getLiter() {
return liter;
}
public double getPreis() {
return preis;
}
public String getDatum() {
return datum;
}
public String setName() {
return name;
}
public double setKilometerstand() {
return kilometerstand;
}
public double setLiter() {
return liter;
}
public double setPreis() {
return preis;
}
public String setDatum() {
return datum;
}
//FIREBASE
}
并尝试检索我之前上传的对象列表。现在,当我尝试拉取List,或使用子方法检索单个值时,它总是返回null。我不知道为什么,请帮忙。
public class Firebase {
static FirebaseDatabase database = FirebaseDatabase.getInstance();
static DatabaseReference lastEntry = database.getReference("LastEntry");
static DatabaseReference eintrag = database.getReference("Eintrag");
static int index;
protected static List<Eintrag> freshLog;
static int LastEntry;
static String test;
public static void lastEntry() {
lastEntry.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
LastEntry=toIntExact((Long) dataSnapshot.getValue(Long.class));
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
}
public Firebase(){
}
protected static void getData(){
eintrag.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
GenericTypeIndicator<List<Eintrag>> e = new GenericTypeIndicator<List<Eintrag>>() {};
// freshLog= dataSnapshot.getValue(e);
test= dataSnapshot.child("3").child("datum").getValue(String.class);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
}
});
}
public static void setData(){
Eintrag.neuerEintrag("R", 23000, 4.5, 6.7, "12.08.99");
Eintrag.neuerEintrag("L", 24000, 3.4, 7.2, "UWE");
Eintrag.neuerEintrag("C", 25000, 42.8, 2.4, "KAI");
Eintrag.neuerEintrag("R", 26000, 4, 5.4, "Burkhardt");
eintrag.setValue(MainActivity.log);
}
}
这是我的Firebase结构: https://imgur.com/p9sGfl0
这是我的调试信息: https://imgur.com/Qq2Aqkk
PS:我知道我的代码很乱,我从学校记得的基础知识的基础知识开始。答案 0 :(得分:0)
要阅读Eintrag节点子节点,您应该这样做:
database.getReference().child("Eintrag")
另外,为了使其更通用,请使用ChildEventListener而不是ValueEventListener。这样您就可以监听Eintrag中的更改,无论是添加的元素,更改的值,删除,移动还是取消。您可以在ChildEventListener中的on方法中分别处理其中一个更改的策略。