gpt4 book ai didi

java - put(String, List) 方法不适用

转载 作者:行者123 更新时间:2023-12-01 13:28:23 26 4
gpt4 key购买 nike

大家好,我的代码中出现了此类错误:

HashMap> 类型中的 put(String, List) 方法不适用于参数 (String, List>)

我的代码在这里:

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 上获取数据以显示在 ListView 上。我使用了 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);
}

}

最佳答案

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

替换:

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

作者:

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

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

关于java - put(String, List<String>) 方法不适用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21691888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com