Galip
Galip

Reputation: 5465

Searching in an Array that is parsed from a XML in Android

I have this XML file which I parse into an ArrayList

In this ArrayList I have countries and the alarmnumbers for the countries in it.

I want to search to a country and get it's police, ambulance or firedep. number.

Here is the code to help you out.

Parsing XML into 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");
      for (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 that will use the 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 I have the landcode, and I want to search in the ArrayList diensten for the numbers that belong with the landcode.

How can I do this?

Upvotes: 0

Views: 298

Answers (3)

Martijn Verburg
Martijn Verburg

Reputation: 3305

Well at the moment you've got an ArrayList of Alarmdiensten objects. I would suggest you might want to change that to a Map so that you are storing a Map of land codes vs your Alarmdiensten objects.

That way you get the Alarmdiensten out of the Map using the landcode and then simply call the getPolitie() etc methods on your Alarmdiensten object.

I would make sure that you encapsulate your Alarmdiensten object BTW, accessing it's private members directly is a bit of a no-no :)

So something 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");
    for (int i = 0; i < 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 get the entry out of the Map

Alarmdiensten land = alarmNumbersForCountries.get(landcode);

Another YMMV point is that you might want to split out the part of your method that builds Alarmdiensten objects from the XML parsing. "Each method should do one thing and one thign well."

Upvotes: 1

Andreas Dolk
Andreas Dolk

Reputation: 114807

Just iterate over the list:

String landcode = getLandCode();
for (Alarmdiensten dienst:diensten) {
  if (dienst.landcode.equals(landcode)) {
    // do what has to be done
  }
}

Consider using a map instead of a list, if you have to lookup the values more frequently:

Map<String, List<Alarmdiensten>> servicesInCountry 
                   = new HashMap<String, List<Alarmdiensten>>();
for (Alarmdiensten dienst:diensten) {
  List<Alarmdiensten> list = servicesInCountry.get(dienst.landcode);
  if (list == null) {
     list = new ArrayList<Alarmdiensten>();
     servicesInCountry.put(dienst.landcode, list);
  }
  list.add(dienst);
}

// ... and later on
List<Alarmdiensten> servicesInSweden = servicesInCountry.get("SWE");

Upvotes: 1

Labeeb Panampullan
Labeeb Panampullan

Reputation: 34833

Use a for loop and search for it

for(Alarmdiensten land :diensten){
        if(land.landcode.equals(landcode) ){
           // yes i got it, The current land. 
            break;
        }
    }

Upvotes: 1

Related Questions