简体   繁体   中英

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

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<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;   //null check
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);   //show back button

        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;
    }

}

The Strings.xml code:

<resources>
    <string name="app_name">Start</string>
    <string name="name">Profile:</string>
    <string name="menu">Menu</string>
    <string name="search">Search</string>
    <string name="group1">Group 1</string>
    <string name="group2">Group 2</string>
    <string name="group3">Group 3</string>
    <string name="group4">Group 4</string>
    <string name="group5">Group 5</string>
    <string-array name="group1">
        <item>item1</item>
        <item>item2</item>
        <item>item3</item>
    </string-array>
    <string-array name="group2">
        <item>item1</item>
        <item>item2</item>
        <item>item3</item>
    </string-array>
    <string-array name="group3">
        <item>item1</item>
        <item>item2</item>
        <item>item3</item>
    </string-array>
    <string-array name="group4">
        <item>item1</item>
        <item>item2</item>
        <item>item3</item>
    </string-array>
    <string-array name="group5">
        <item>item1</item>
        <item>item2</item>
        <item>item3</item>
    </string-array>


</resources>

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM