我正在使用XML解析器。因为我必须将一个id从一个活动传递给另一个活动,但是这个id没有显示在第一个屏幕的textview中。我怎样才能将这个id传递给其他屏幕。
这是我的代码:
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
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_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_LASTVALUE,parser.getValue(e, KEY_LASTVALUE));
map.put(KEY_CHANGE, parser.getValue(e, KEY_CHANGE));
map.put(KEY_STKEXCHANGE, parser.getValue(e, KEY_STKEXCHANGE));
map.put(KEY_LASTPRICE, parser.getValue(e, KEY_LASTPRICE));
// adding HashList to ArrayList
menuItems.add(map);
}
这里我必须在点击特定元素值时传递id,我该怎么做:
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
// String name = ((TextView) view.findViewById(R.id.name_label)).getText().toString();
// String cost = ((TextView) view.findViewById(R.id.cost_label)).getText().toString();
// String description = ((TextView) view.findViewById(R.id.description_label)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), IndicesDetails.class);
// in.putExtra(KEY_STKEXCHANGE, name);
// in.putExtra(KEY_COST, cost);
// in.putExtra(KEY_DESC, description);
// string ids = getItemId(position);
// in.putExtra("idvalue", id);
startActivity(in);
}
});
答案 0 :(得分:0)
在当前活动中:
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Starting new intent
Intent in = new Intent(getApplicationContext(), IndicesDetails.class);
in.putExtra("idvalue", id);
startActivity(in);
}
然后在新活动中,检索这些值:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
long value = extras.getLong("idvalue");
}
答案 1 :(得分:0)
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap map=yourArrayList.get(position);
String id=map.get("Your key");
in.putExtra("idvalue", id);
}