java - Searching in an Array that is parsed from a XML in Android -
i have xml file parse arraylist
in arraylist have countries , alarmnumbers countries in it.
i want search country , it's police, ambulance or firedep. number.
here code out.
parsing xml arraylist:
protected arraylist<alarmdiensten> getalarmdiensten() { arraylist<alarmdiensten> lijst = new arraylist<alarmdiensten>(); try { documentbuilder builder =documentbuilderfactory.newinstance().newdocumentbuilder(); document doc = builder.parse(getassets().open("alarmdiensten.xml")); nodelist nl = doc.getelementsbytagname("land"); (int i=0;i<nl.getlength();i++) { node node = nl.item(i); alarmdiensten land = new alarmdiensten(); land.land = xml.innerhtml(xml.getchildbytagname(node, "naam")); land.landcode = xml.innerhtml(xml.getchildbytagname(node, "code")); land.politie = xml.innerhtml(xml.getchildbytagname(node, "politie")); land.ambulance = xml.innerhtml(xml.getchildbytagname(node, "ambulance")); land.brandweer = xml.innerhtml(xml.getchildbytagname(node, "brandweer")); land.telamba = xml.innerhtml(xml.getchildbytagname(node, "telamba")); land.adresamba = xml.innerhtml(xml.getchildbytagname(node, "adresamba")); lijst.add(land); } } catch (exception e) {; } return lijst; }
the method use alarmnumbers:
public void alarmmenu(){ string landcode; arraylist<alarmdiensten> diensten = getalarmdiensten(); if(fakelocation = true) { landcode = sfakelocation; } else { try { landcode = getaddressforlocation(this, locationnow).getcountrycode(); } catch (ioexception e) { e.printstacktrace(); } }
so have landcode, , want search in arraylist diensten numbers belong landcode.
how can this?
well @ moment you've got arraylist of alarmdiensten objects. suggest might want change map storing map of land codes vs alarmdiensten objects.
that way alarmdiensten out of map using landcode , call getpolitie() etc methods on alarmdiensten object.
i make sure encapsulate alarmdiensten object btw, accessing it's private members directly bit of no-no :)
so like:
protected map<string, alarmdiensten> getalarmdiensten() { map<string, alarmdiensten> alarmnumbersforcountries = new hashmap<string, alarmdiensten>(); try { documentbuilder builder = documentbuilderfactory.newinstance().newdocumentbuilder(); document doc = builder.parse(getassets().open("alarmdiensten.xml")); nodelist nl = doc.getelementsbytagname("land"); (int = 0; < nl.getlength(); i++) { node node = nl.item(i); alarmdiensten land = new alarmdiensten(); land.setland(xml.innerhtml(xml.getchildbytagname(node, "naam"))); land.setlandcode(xml.innerhtml(xml.getchildbytagname(node, "code"))); land.setpolitie(xml.innerhtml(xml.getchildbytagname(node, "politie"))); land.setambulance(xml.innerhtml(xml.getchildbytagname(node, "ambulance"))); land.setbrandweer(xml.innerhtml(xml.getchildbytagname(node, "brandweer"))); land.settelamba(xml.innerhtml(xml.getchildbytagname(node, "telamba"))); land.setadresamba(xml.innerhtml(xml.getchildbytagname(node, "adresamba"))); alarmnumbersforcountries.put(land.getlandcode(), land); } } catch (exception e) { // handle exception } return alarmnumbersforcountries; }
to entry out of map
alarmdiensten land = alarmnumbersforcountries.get(landcode);
another ymmv point might want split out part of method builds alarmdiensten objects xml parsing. "each method should 1 thing , 1 thign well."
Comments
Post a Comment