Error: put(java.lang.string, list<string>) in hashmap cannot be applied to (java.util.list<java.lang.string>)

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

Error: put(java.lang.string, list<string>) in hashmap cannot be applied to (java.util.list<java.lang.string>)

问题

以下是翻译好的内容:

package com.mystartup.start;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.ExpandableListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MenuActivity extends AppCompatActivity {
    ExpandableListView expandableListView;
    List<String> listGroup;
    HashMap<String, List<String>> listItem;
    MainAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        assert getSupportActionBar() != null;   // 空指针检查
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);   // 显示返回按钮

        expandableListView = findViewById(R.id.expanded_listview);
        listGroup = new ArrayList<>();
        listItem = new HashMap<>();
        adapter = new MainAdapter(this, listGroup, listItem);
        expandableListView.setAdapter(adapter);
        initListData();
    }

    private void initListData() {
        listGroup.add(getString(R.string.group1));
        listGroup.add(getString(R.string.group2));
        listGroup.add(getString(R.string.group3));
        listGroup.add(getString(R.string.group4));
        listGroup.add(getString(R.string.group5));

        String[] array;

        List<String> list1 = new ArrayList<>();
        array = getResources().getStringArray(R.array.group1);
        for (String item : array){
            list1.add(item);
        }
        List<String> list2 = new ArrayList<>();
        array = getResources().getStringArray(R.array.group2);
        for (String item : array){
            list2.add(item);
        }
        List<String> list3 = new ArrayList<>();
        array = getResources().getStringArray(R.array.group3);
        for (String item : array){
            list3.add(item);
        }
        List<String> list4 = new ArrayList<>();
        array = getResources().getStringArray(R.array.group4);
        for (String item : array){
            list4.add(item);
        }
        List<String> list5 = new ArrayList<>();
        array = getResources().getStringArray(R.array.group5);
        for (String item : array){
            list5.add(item);
        }
        listItem.put(listGroup.get(0), list1);
        listItem.put(listGroup.get(1), list2);
        listItem.put(listGroup.get(2), list3);
        listItem.put(listGroup.get(3), list4);
        listItem.put(listGroup.get(4), list5);
        adapter.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);

        return true;
    }
    @Override
    public boolean onSupportNavigateUp(){
        finish();
        return true;
    }
}

Strings.xml 代码部分没有需要翻译的内容。

英文:

I'm a beginner following a tutorial and just can't understand what I'm doing wrong. I tried to ask the owner of the tutorial without any luck. I'm receiving the error mentioned in the title when trying to build and expendable menu. The listGroup gives the error from line 72 to 76 (listItem.put(listGroup).get(0),list1;). Looks like I'm using the HashMap wrongly. I'm using Android Studios 3.6.1. Please help me out. Thank you in advance!

package com.mystartup.start;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.ExpandableListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MenuActivity extends AppCompatActivity {
ExpandableListView expandableListView;
List&lt;String&gt; listGroup;
HashMap&lt;String,List&lt;String&gt;&gt; listItem;
MainAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
assert getSupportActionBar() != null;   //null check
getSupportActionBar().setDisplayHomeAsUpEnabled(true);   //show back button
expandableListView = findViewById(R.id.expanded_listview);
listGroup = new ArrayList&lt;&gt;();
listItem = new HashMap&lt;&gt;();
adapter = new MainAdapter(this,listGroup,listItem);
expandableListView.setAdapter(adapter);
initListData();
}
private void initListData() {
listGroup.add(getString(R.string.group1));
listGroup.add(getString(R.string.group2));
listGroup.add(getString(R.string.group3));
listGroup.add(getString(R.string.group4));
listGroup.add(getString(R.string.group5));
String[] array;
List&lt;String&gt; list1 = new ArrayList&lt;&gt;();
array = getResources().getStringArray(R.array.group1);
for (String item : array){
list1.add(item);
}
List&lt;String&gt; list2 = new ArrayList&lt;&gt;();
array = getResources().getStringArray(R.array.group2);
for (String item : array){
list2.add(item);
}
List&lt;String&gt; list3 = new ArrayList&lt;&gt;();
array = getResources().getStringArray(R.array.group3);
for (String item : array){
list3.add(item);
}
List&lt;String&gt; list4 = new ArrayList&lt;&gt;();
array = getResources().getStringArray(R.array.group4);
for (String item : array){
list4.add(item);
}
List&lt;String&gt; list5 = new ArrayList&lt;&gt;();
array = getResources().getStringArray(R.array.group5);
for (String item : array){
list5.add(item);
}
listItem.put(listGroup).get(0),list1;
listItem.put(listGroup).get(1),list2;
listItem.put(listGroup).get(2),list3;
listItem.put(listGroup).get(3),list4;
listItem.put(listGroup).get(4),list5;
adapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
@Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
}

The Strings.xml code:

&lt;resources&gt;
&lt;string name=&quot;app_name&quot;&gt;Start&lt;/string&gt;
&lt;string name=&quot;name&quot;&gt;Profile:&lt;/string&gt;
&lt;string name=&quot;menu&quot;&gt;Menu&lt;/string&gt;
&lt;string name=&quot;search&quot;&gt;Search&lt;/string&gt;
&lt;string name=&quot;group1&quot;&gt;Group 1&lt;/string&gt;
&lt;string name=&quot;group2&quot;&gt;Group 2&lt;/string&gt;
&lt;string name=&quot;group3&quot;&gt;Group 3&lt;/string&gt;
&lt;string name=&quot;group4&quot;&gt;Group 4&lt;/string&gt;
&lt;string name=&quot;group5&quot;&gt;Group 5&lt;/string&gt;
&lt;string-array name=&quot;group1&quot;&gt;
&lt;item&gt;item1&lt;/item&gt;
&lt;item&gt;item2&lt;/item&gt;
&lt;item&gt;item3&lt;/item&gt;
&lt;/string-array&gt;
&lt;string-array name=&quot;group2&quot;&gt;
&lt;item&gt;item1&lt;/item&gt;
&lt;item&gt;item2&lt;/item&gt;
&lt;item&gt;item3&lt;/item&gt;
&lt;/string-array&gt;
&lt;string-array name=&quot;group3&quot;&gt;
&lt;item&gt;item1&lt;/item&gt;
&lt;item&gt;item2&lt;/item&gt;
&lt;item&gt;item3&lt;/item&gt;
&lt;/string-array&gt;
&lt;string-array name=&quot;group4&quot;&gt;
&lt;item&gt;item1&lt;/item&gt;
&lt;item&gt;item2&lt;/item&gt;
&lt;item&gt;item3&lt;/item&gt;
&lt;/string-array&gt;
&lt;string-array name=&quot;group5&quot;&gt;
&lt;item&gt;item1&lt;/item&gt;
&lt;item&gt;item2&lt;/item&gt;
&lt;item&gt;item3&lt;/item&gt;
&lt;/string-array&gt;
&lt;/resources&gt;

答案1

得分: 0

你在这里使用了错误的语法:

listItem.put(listGroup).get(0),list1;

Map API 中的 put 方法需要两个参数。这两个参数都需要用括号括起来,如下所示:

listItem.put((listGroup).get(0), list1);
英文:

You are using the wrong syntax here:

listItem.put(listGroup).get(0),list1;

The method put from the Map API, takes 2 argumets. Both of those arguments need to be wrapped, as such:

listItem.put((listGroup).get(0), list1);

huangapple
  • 本文由 发表于 2020年4月6日 22:03:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61061622.html
匿名

发表评论

匿名网友

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

确定