Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!