Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void setMoreViewed(String itemViewed, String user) {
- refViewed = data.getReference("/cliente/" + user + "/top5");
- Map<Integer, String> top5_map = new HashMap<>();
- refViewed.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot ds) {
- for (DataSnapshot tmp : ds.getChildren()) {
- top5_map.put(Integer.parseInt(tmp.getKey()), tmp.getValue(String.class));
- }
- switch (top5_map.size()) {
- case 0:
- top5_map.put(1, itemViewed);
- break;
- case 1:
- if (!top5_map.containsValue(itemViewed)) {
- top5_map.put(2, top5_map.get(1));
- top5_map.replace(1, itemViewed);
- }
- break;
- case 2:
- if (!top5_map.containsValue(itemViewed)) {
- top5_map.put(3, top5_map.get(2));
- top5_map.replace(2, top5_map.get(1));
- top5_map.replace(1, itemViewed);
- }
- break;
- case 3:
- if (!top5_map.containsValue(itemViewed)) {
- top5_map.put(4, top5_map.get(3));
- top5_map.replace(3, top5_map.get(2));
- top5_map.replace(2, top5_map.get(1));
- top5_map.replace(1, itemViewed);
- } else {
- top5_map.replace(1, itemViewed);
- }
- break;
- case 4:
- if (!top5_map.containsValue(itemViewed)) {
- top5_map.put(5, top5_map.get(4));
- top5_map.replace(4, top5_map.get(3));
- top5_map.replace(3, top5_map.get(2));
- top5_map.replace(2, top5_map.get(1));
- top5_map.replace(1, itemViewed);
- } else {
- top5_map.replace(1, itemViewed);
- }
- break;
- case 5:
- if (!top5_map.containsValue(itemViewed)) {
- top5_map.replace(5, top5_map.get(4));
- top5_map.replace(4, top5_map.get(3));
- top5_map.replace(3, top5_map.get(2));
- top5_map.replace(2, top5_map.get(1));
- top5_map.replace(1, itemViewed);
- } else {
- top5_map.replace(1, itemViewed);
- }
- break;
- }
- for (Map.Entry<Integer, String> entry : top5_map.entrySet()) {
- refViewed.child(String.valueOf(entry.getKey())).setValue(entry.getValue());
- }
- }
- @Override
- public void onCancelled(DatabaseError de) {
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement