I'm trying to make a rating system for my app but I can only rate once and can't update it.
The way it works is that when I click 3 stars (out of 5 stars) from the app, I'm expecting that it would pass the value(3) from DBqueries
as initialRating
unto ProductDetailsActivity
where it updates the ratings from Firebase where I can see how many ratings on 1_star, 2_star, 3_star, 4_star and 5_star.
Here's the code from ProductDetailsActivity.java
:
Map<String, Object> updateRating = new HashMap<>();
if (DBqueries.myRatedIds.contains(productID)) {
updateRating.put(initialRating+1+"_star", (long)documentSnapshot.get(initialRating+1+"_star")-1);
updateRating.put(starPosition + 1 + "_star", (long)documentSnapshot.get(starPosition+1+"_star")+1);
updateRating.put("average_rating", String.valueOf(calculateAverageRating((long)starPosition+1)));
}
Apparently the line that's causing the error is this:
updateRating.put(initialRating+1+"_star", (long)documentSnapshot.get(initialRating+1+"_star")-1);
The app somehow works if I replace initial rating with a value, e.g.
updateRating.put(initialRating+1+"_star", (long)documentSnapshot.get(1+1+"_star")-1);
Only if the initialRating
is used it gives an error.
Here's my DBqueries.java
:
if (task.getResult().get("product_ID_" + x).toString().equals(ProductDetailsActivity.productID)) {
ProductDetailsActivity.initialRating = Integer.parseInt(String.valueOf((long) task.getResult().get("rating_"+x))) - 1;
if (ProductDetailsActivity.rateContainer != null) {
ProductDetailsActivity.setRating(ProductDetailsActivity.initialRating);
}
}
I tried giving initalRating
a constant value from the code above to see if it works but it still give the error:
ProductDetailsActivity.initialRating = 2;
I'm still a beginner on Android studio so I really want to understand why am I getting the error. I've been spending my day trying to figure out this problem. I hope you guys can help me. Thanks!