Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
Android Firebase getting data error

every time i try to get data from firebase Motel by Motel it just doesnt work but when i try to get just a String it works fine !! i dont know where the problem is ,this is the ChildEventListener code:

        refe.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

           motels.clear();
    for(DataSnapshot snapshot : snap.getChildren())
    {
      Motel motel =  snap.getValue(Motel.class);
        motels.add(motel);
    }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {


        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {


        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

and the Motel class code :

   public class Motel {
   private String name,adresse,price;
private Long stars;
private Long chambers;
private String region;

public Motel(String name, Long chambers,String adresse, String price,  String region,Long stars )
{
    this.name=name;
    this.adresse=adresse;
    this.price=price;
    this.stars=stars;
    this.chambers=chambers;
    this.region=region;

}

public String getRegion() {
    return region;
}

public void setRegion(String region) {
    this.region = region;
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Long getChambers() {
    return chambers;
}

public void setChambers(Long chambers) {
    this.chambers = chambers;
}

plz help me !! i cant seem to figure out what's the problem

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You should post the Log so we can see the problem, but I think I already know whats happening.

The method that brings all items together is OnDataChange from ValueEventListener, meanwhile onChildAdded from ChildEventListener only take 1 at a time, and it's executed all the times needed.

Change

motels.clear();
for(DataSnapshot snapshot : snap.getChildren()) {
    Motel motel =  snap.getValue(Motel.class);
    motels.add(motel);
}
adapter.notifyDataSetChanged();

For

motels.clear();
Motel motel =  dataSnapshot.getValue(Motel.class);
motels.add(motel);
adapter.notifyDataSetChanged();
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda