For my application, I added a ViewPager2 to show the recommended containing image, name, and country. But when I try to retrieve data from firebase this error shows. I tried tweaking the code but I think I'm only making it worse because the error is replaced by No setter/field for a class error. I have been stuck with the same error for a few days now and still can't solve it. I badly need help.
Below is my code for MainActivity
database = FirebaseDatabase.getInstance().getReference();
DatabaseReference myRef = database.child("Packages");
recommendList = new ArrayList<Recommendation>();
adapter = new RecommendAdapter(this, recommendList);
recommendViewPager.setAdapter(adapter);
setupRecommendViewPager();
myRef.addValueEventListener(new ValueEventListener() {
List<Recommendation> recommendList = new ArrayList<>();
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
Recommendation recommendation = dataSnapshot.getValue(Recommendation.class);
//String recommendation = dataSnapshot.getValue(String.class);
recommendList.add(recommendation);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
And this is my Recommendation.class
public class Recommendation {
public String package_attractions;
public String package_availability;
public String package_country;
public String package_description;
public String package_name;
public String package_photos;
public Double package_price;
public Double package_rating;
public String package_region;
public String package_video;
public Recommendation(){}
public Recommendation(String package_attractions, String package_availability, String package_country, String package_description, String package_name, String package_photos, Double package_price, Double package_rating, String package_region, String package_video) {
this.package_attractions = package_attractions;
this.package_availability = package_availability;
this.package_country = package_country;
this.package_description = package_description;
this.package_name = package_name;
this.package_photos = package_photos;
this.package_price = package_price;
this.package_rating = package_rating;
this.package_region = package_region;
this.package_video = package_video;
}
public String getPackage_attractions() {
return package_attractions;
}
public void setPackage_attractions(String package_attractions) {
this.package_attractions = package_attractions;
}
public String getPackage_availability() {
return package_availability;
}
public void setPackage_availability(String package_availability) {
this.package_availability = package_availability;
}
.........
See, you can dig deeper to get data, which basically means that this field it's not a String but an object (map), since each node in the Realtime Database is composed of key-value pairs.
When you're using the following line of code:
Recommendation recommendation = dataSnapshot.getValue(Recommendation.class);
It means that you're trying to map each node under "Packages" into an object of type Recommendation
. This means that each field in the database should have the exact same type as in your class, which isn't happening. Your package_photos
field in the database is a map, while in the class is a string, hence the error.
To solve this, you should change the type of your package_photos
field inside the class to be either an object or a map. Since I cannot see the content of your package_photos
node, I can only assume that you have a key that is named "url" and a value that is represented by the actual value of the URL, "https://...".
So it most likely it looks similar to this:
Packages
|
--- BG0PH108
|
--- package_photos
|
--- url: "https://..."
So in this case, you need to create a class with at a minimum declaration, that looks like this:
public class PackagePhotos {
public String url;
}
Or if you want to use encapsulation:
public class PackagePhotos {
private String url;
public PackagePhotos() {}
public PackagePhotos(String url) {
this.url = url;
}
public getUrl() {
return url;
}
}
And inside the Recommendation
class, the field should be changed to:
public class Recommendation {
//...