1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
public class AndroidXMLParsingActivity extends ListActivity {
// All static variables
static final String URL = "http://news.abidjan.net/apps/iphone/une.xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_TITLE = "title";
static final String KEY_AUTHOR = "author";
static final String KEY_PUBDATE = "pubDate";
static final String KEY_DESC = "description";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
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_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_AUTHOR, parser.getValue(e, KEY_AUTHOR));
map.put(KEY_PUBDATE, parser.getValue(e, KEY_PUBDATE));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_AUTHOR, KEY_TITLE, KEY_PUBDATE }, 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_TITLE, name);
// in.putExtra(KEY_PUBDATE, cost);
// in.putExtra(KEY_TITLE, description);
// startActivity(in);
//
// }
// });
}
} |
Partager