方法put(String,List <string>)不适用</string>

时间:2014-02-11 02:30:38

标签: java

大家好我的代码上有这种错误:

方法put(String,List)在HashMap&gt;类型中;不适用于参数(String,List&gt;)

我的代码在这里:

private void prepareListData() {


    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("VRP Medical Bay");
    //listDataHeader.add("");
    //listDataHeader.add("");

    // Adding child data
    List<HashMap<String, String>> listUnderVRP = new ArrayList<HashMap<String, String>>();
    //List<String> listUnderVRP = new ArrayList<String>();
    com.test.utilities.XMLParser parser = new com.test.utilities.XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SONG);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

        // adding HashList to ArrayList
        listUnderVRP.add(map);

    }


    listDataChild.put(listDataHeader.get(0), listUnderVRP); // Header, Child data
    //listDataChild.put(listDataHeader.get(1), nowShowing);
    //listDataChild.put(listDataHeader.get(2), comingSoon);
}

我在put函数上遇到错误。

继承了我的代码的整个逻辑,我有问题将我的xml文件集成到我的listview。流程是这样的:我有导航抽屉里面我有可扩展的列表,在扩展名单里面我有listview,我得到我的xml api上的数据显示在列表view.ive工作3天但没有运气

public class HomeActivity extends Activity {

static final String URL = "http://api.test.info/test/test.xml";
// XML node keys
static final String KEY_SONG = "elements"; // parent node
static final String KEY_ID = "id";
public static final String KEY_TITLE = "title";
public static final String KEY_THUMB_URL = "thumb_url";

ExpandableListAdapter listAdapter;
ExpandableListView expListView;

public DrawerLayout drawer;
ImageView navDrawerBtn;

HashMap<String, List<String>> listDataChild;
List<String> listDataHeader;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }


    navDrawerBtn = (ImageView)findViewById(R.id.headerDrawer);
    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        expListView.setIndicatorBounds(402,465);    
    } else {        
        expListView.setIndicatorBoundsRelative(402,465);    
    } 

    drawer = (DrawerLayout)findViewById(R.id.drawer_layout);

    prepareListData();



    navDrawerBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if(!drawer.isDrawerOpen(expListView)) {
                drawer.openDrawer(expListView);
                } else {
                    drawer.closeDrawer(expListView);
                }

            }
        });


    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);

    // Listview Group click listener
    expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            // Toast.makeText(getApplicationContext(),
            // "Group Clicked " + listDataHeader.get(groupPosition),
            // Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    // Listview Group expanded listener
    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Expanded",
                    Toast.LENGTH_SHORT).show();
        }
    });

    // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Collapsed",
                    Toast.LENGTH_SHORT).show();

        }
    });

    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub
            Toast.makeText(
                    getApplicationContext(),
                    listDataHeader.get(groupPosition)
                            + " : "
                            + listDataChild.get(
                                    listDataHeader.get(groupPosition)).get(
                                    childPosition), Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
    });
}

/*
 * Preparing the list data
 */
private void prepareListData() {


    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("VRP Medical Bay");
    //listDataHeader.add("");
    //listDataHeader.add("");

    // Adding child data
    List<HashMap<String, String>> listUnderVRP = new ArrayList<HashMap<String, String>>();
    //List<String> listUnderVRP = new ArrayList<String>();
    com.test.utilities.XMLParser parser = new com.test.utilities.XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SONG);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

        // adding HashList to ArrayList
        listUnderVRP.add(map);

    }


    listDataChild.put(listDataHeader.get(0), listUnderVRP); // Header, Child data
    //listDataChild.put(listDataHeader.get(1), nowShowing);
    //listDataChild.put(listDataHeader.get(2), comingSoon);
}

 public int GetPixelFromDips(float pixels) {
        // Get the screen's density scale 
        final float scale = getResources().getDisplayMetrics().density;
        // Convert the dps to pixels, based on density scale
        return (int) (pixels * scale + 0.5f);
    }

}

3 个答案:

答案 0 :(得分:1)

主要原因是你的listDataChild只允许List<String>类型,但你的输入参数(listUnderVRP)有List<HashMap<String, String>>所以试试这个:

取代:

listDataChild = new HashMap<String, List<String>>();

by:

listDataChild = new ArrayList<HashMap<String, String>>();

我根据您提供的内容,不确定它是否适合您的业务逻辑。

答案 1 :(得分:0)

您正在将HashMap构建为HashMap<String, String>而不是HashMap<String, List<?>>

HashMap<String, String> map = new HashMap<String, String>();

据推测,解析器会返回某种类型的List,因此您会遇到类型错误。

答案 2 :(得分:0)

要解决的代码太多了,但根据您的意见:

  

类型List&gt;中的get(int)方法不适用于参数(String)

get(i) i String intput(K, V)时,会导致调用add()。{/ 1}。

  

方法put(String,ArrayList&gt;)未定义类型ArrayList&gt;

尝试在列表上使用 Map 方法{{1}}会造成错误,该方法只有{{1}}方法。