package com.androidhive.xmlparsing; import java.util.ArrayList; import java.util.HashMap; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; public class AndroidXMLParsingActivity extends ListActivity { // All static variables static final String URL = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%28select%20woeid%20from%20geo.places%20where%20text%3D%22Delhi%22%29&diagnostics=true"; // XML node keys static final String KEY_ITEM = "item"; // parent node static final String KEY_ID = "title"; static final String KEY_NAME = "geo:lat"; static final String KEY_COST = "geo:long"; static final String KEY_DESC = "yweather:forecast"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList> menuItems = new ArrayList>(); XMLParser parser = new XMLParser(); String xml = parser.getXmlFromUrl(URL); // getting XML Log.d("xml",xml); Document doc = parser.getDomElement(xml); // getting DOM element Log.d("doc",""+doc); Log.d("parser.getDomElement(xml)",""+parser.getDomElement(xml)); NodeList nl = doc.getElementsByTagName(KEY_ITEM); Log.d("parser.getElementValue(doc);",""+parser.getElementValue(doc)); parser.getElementValue(doc); // looping through all item nodes for (int i = 0; i < nl.getLength(); i++) { // creating new HashMap HashMap map = new HashMap(); Element e = (Element) nl.item(i); // adding each child node to HashMap key => value map.put(KEY_ID, parser.getValue(e, KEY_ID)); Log.d("KEY_ID",parser.getValue(e, KEY_ID)); map.put(KEY_NAME, parser.getValue(e, KEY_NAME)); Log.d("KEY_NAME",parser.getValue(e, KEY_NAME)); map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST)); Log.d("KEY_COST",parser.getValue(e, KEY_COST)); map.put(KEY_DESC, parser.getValue(e, KEY_DESC)); Log.d("KEY_DESC",parser.getValue(e, KEY_DESC)); Log.d("map",""+map); // adding HashList to ArrayList menuItems.add(map); Log.d("menuItems",""+menuItems); } // Adding menuItems to ListView ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.list_item, new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] { R.id.name, R.id.desciption, R.id.cost }); setListAdapter(adapter); // selecting single ListView item ListView lv = getListView(); 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)).getText().toString(); String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString(); String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString(); // Starting new intent Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class); in.putExtra(KEY_NAME, name); in.putExtra(KEY_COST, cost); in.putExtra(KEY_DESC, description); startActivity(in); } }); } }