I have a very basic firebase database filled with movies. It's basically the movie title, its year and genre as shown below

I also have a class for these 3 attributes of each key. All I want to do is take the data from the firebase snapshot and put it inside my object.
What I did was:
Initialize the database for the child "Filme":
DatabaseReference database = FirebaseDatabase.getInstance().getReference().child("Filme");Then I added an ValueEvent Listener:
database.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot keyNode : dataSnapshot.getChildren()) { Filme nfilme = keyNode.getValue(Filme.class); String s = nfilme.getTituloFilme(); // get title }
The debugger shows the line:
DataSnapshot { key = 001, value = {Genero=Drama, Titulo=Titanic, Ano=2000} }
Which shows that it was able to extract the data, however when I call the getTitle() method it returns null, why is this happening?
EDIT: class added
public class Filme {
private String tituloFilme;
private String genero;
private String ano;
public Filme(){
}
public Filme(String tituloFilme, String genero, String ano){
this.tituloFilme = tituloFilme;
this.genero = genero;
this.ano = ano;
}
public String getTituloFilme() {
return tituloFilme;
}
public void setTituloFilme(String tituloFilme) {
this.tituloFilme = tituloFilme;
}
public String getGenero() {
return genero;
}
public void setGenero(String genero) {
this.genero = genero;
}
public String getAno() {
return ano;
}
public void setAno(String ano) {
this.ano = ano;
}
}
CodePudding user response:
try to add this simple if
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot keyNode : dataSnapshot.getChildren())
{
if(keyNode.getKey().equals("Titulo")){
Filme nfilme = keyNode.getValue();
//do what you want
}
}
if thats not gonna work then, try to add another snapshot
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot keyNode : dataSnapshot.getChildren())
{
for(DataSnapshot child2 : keyNode.getChildren()){
if(keyNode.getKey().equals("Titulo")){
Filme nfilme = keyNode.getValue();
//do what you want
}
}
}
don't forget to use toString() method
CodePudding user response:
Firebase uses JavaBean property naming conventions for its JSON to Java conversions (and vice versa).
This means that (for example) public String getTituloFilme() in your code, maps to a property tituloFilme in the JSON. That mismatches on two fronts with your actual database:
- There is no
Filmesuffix in your database. - The property in the database is
Titulo, while the Java class expectstitulo.
The easiest way to fix this is to specify type annotations in your Java class to explicitly map to specific JSON properties:
public class Filme {
private String tituloFilme;
private String genero;
private String ano;
public Filme(){ }
public Filme(String tituloFilme, String genero, String ano){
this.tituloFilme = tituloFilme;
this.genero = genero;
this.ano = ano;
}
@PropertyName("Titulo")
public String getTituloFilme() {
return tituloFilme;
}
@PropertyName("Titulo")
public void setTituloFilme(String tituloFilme) {
this.tituloFilme = tituloFilme;
}
@PropertyName("Genero")
public String getGenero() {
return genero;
}
@PropertyName("Genero")
public void setGenero(String genero) {
this.genero = genero;
}
@PropertyName("Ano")
public String getAno() {
return ano;
}
@PropertyName("Ano")
public void setAno(String ano) {
this.ano = ano;
}
}
