HashMap<String, Boolean> getValue返回Boolean,但Java尝试将其强制转换为String。

huangapple go评论82阅读模式
英文:

HashMap<String, Boolean> getValue returns Boolean but java tries to cast to String

问题

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

for (Sound s: drumThumper.soundsToe) {
    HashMap<String, Boolean> l = new HashMap<>();
    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);
});
英文:
    ArrayList&lt;HashMap&lt;String, Boolean&gt;&gt; list = new ArrayList&lt;&gt;();

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

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

    listPopupWindow.setOnItemClickListener((parent, view1, position, id) -&gt; {
        HashMap&lt;String, Boolean&gt; item = list.get(position);
        Map.Entry&lt;String,Boolean&gt; 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

答案1

得分: 2

你在使用HashMap时收到了关于使用原始类型的警告,而警告正是因为这个原因存在。当你写下:

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

你正在将一个String值放入映射中,尽管你之前承诺只放入Boolean值。

我无法详细建议如何重新修改这段代码,因为你的意图并不完全清楚,但我会指出,通常使用Set<K>比使用Map<K, Boolean>更容易。

英文:

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(&quot;name&quot;, s.getSound().replace(&quot;.wav&quot;, &quot;&quot;));

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&lt;K&gt; instead of a Map&lt;K, Boolean&gt;.

答案2

得分: 2

你的问题在这里:

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

你在HashMap中放入了两个字符串。当你尝试获取布尔值时,它找到了一个字符串。

英文:

your problem is here:

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

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

huangapple
  • 本文由 发表于 2020年10月15日 02:22:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64359349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定