KaJasB KaJasB - 3 days ago 6
Android Question

Firebase: datasnapshot.getValue(className) not working

I'm new to Firebase. I try to get data from dataSnapshot by using getValue(className). But no matter i try object return null.
I got my class and directories like below
Model: packageName/Models/TongTin.class
Activity: packageName/Activities/MainActivity.class

Here my model code:

@IgnoreExtraProperties
public class TongTin {

private String type;
private String lotPrice;
private String note;
private String user_uid;
private DatabaseReference databaseReference;

public TongTin() {

}

public TongTin(String type, String lotPrice, String note, String user_uid) {
databaseReference = FirebaseDatabase.getInstance().getReference();
this.type = type;
this.lotPrice = lotPrice;
this.note = note;
this.user_uid = user_uid;
}

public String getUserUid() {
return user_uid;
}

public String getType() {
return type;
}

public String getNote() {
return note;
}

public void addTongTin() {
String key = databaseReference.child(TongTinTable.tableName).push().getKey();
Map<String,Object> postValues=toMap();
Map<String,Object> childUpdates=new HashMap<>();
childUpdates.put(TongTinTable.postURL+key,postValues);
databaseReference.updateChildren(childUpdates);
}

}


Here onDataChange():

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
TongTin tongTin = dataSnapshot.getValue(TongTin.class);
Log.d("Tong Tin",tongTin.getNote());
}

Answer

Might be case that you're getting back collection...if so you'll need to do something like

for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) 
Comments