-1
    ArrayList<HashMap<String, Boolean>> list = new ArrayList<>();

    for (Sound s: drumThumper.soundsToe) {
        HashMap l = new HashMap<String, Boolean>();
        l.put("name", s.getSound().replace(".wav", ""));
        l.put("checkbox", s==drumThumper.selectedSoundToe);
        list.add(l);
    }

    SimpleAdapter simpleAdapter = new SimpleAdapter(activity.getApplicationContext(), list, R.layout.checkbox_list, new String[]{"name", "checkbox"}, new int[]{R.id.name, R.id.checkbox});
    listPopupWindow.setAdapter(simpleAdapter);

    listPopupWindow.setOnItemClickListener((parent, view1, position, id) -> {
        HashMap<String, Boolean> item = list.get(position);
        Map.Entry<String,Boolean> entry = item.entrySet().iterator().next();
        Boolean oldValue = entry.getValue();
        entry.setValue(!oldValue);
    });

but I'm getting java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean at lambda$onToeLongClick$1(Dialogues.java:165) on

Boolean oldValue = entry.getValue();

How can that be possible? The HashMap is obviously from String to Boolean. And I'm calling .getValue which even the intellisense says returns a Boolean

Guerlando OCs
  • 1,886
  • 9
  • 61
  • 150
  • Start here: `HashMap l ` ... that is a raw type. Dont use that: https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – GhostCat Oct 14 '20 at 18:25

2 Answers2

2

You're getting a warning about using a raw type where you have HashMap listed, and the warning is there for exactly this reason. When you say

l.put("name", s.getSound().replace(".wav", ""));

you are putting a String value into the map even though you promised only to put Booleans in it.

I can't advise on how exactly to best rework this code, since it's not entirely clear what your intention is, but I will note that it's usually easier to use a Set<K> instead of a Map<K, Boolean>.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
2

your problem is here:

l.put("name", s.getSound().replace(".wav", ""));

you put inside HashMap 2 strings. And when you try to get boolean value, he find a string.

Francesco Bocci
  • 827
  • 8
  • 19