I want to get all list of restaurants from Firebase in android.
Here is my code:
boolean delivery;
String openTime, closeTime, restaurantName;
long
Pre-requisit: We are setting the Snapshot response to our model class objects.
In our Model class. there must be one or more objects which we are setting as list.
Now in response, the ArrayList inside the Snapshot is coming to us as hashmap but we need list.
So the Solution i find is:
set the first layer of data to model objects as it is using limited parameter constructor.
Then the ArrayList object - Get it as a separate object of that specific class and then add it into the model class by getter.
Follow the below code sample.
Lets suppose you have Object Model class. And InnterList as an array list. My solution is to take innerList as a seperate object and then add it to model class using constructors.
If this list is not comming in response. You can handle them using limited constructor parameters.
Object object;
FirebaseDatabase.getInstance().getReference().getRoot().child("yourKey").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot temp : snapshot.getChildren()) {
if (temp.hasChild("InnerList")) {
List innerList = new ArrayList();
DataSnapshot yourInnerArrayListSnapShot = temp.child("InnerList");
for (DataSnapshot innerTemp : yourInnerArrayListSnapShot.getChildren()) {
InnerList yourInnerArrayListObject = innerTemp.getValue(InnerList.class);
innerList.add(yourInnerArrayListObject);
}
object = new Object(a, b, innerList);
} else {
object = new Object(a, b);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.d(TAG, "onCancelled error is : " + error.getMessage());
pd.dismiss();
}
});
Enjoy !!