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
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();