Android Hashmap no pudo aparecer dentro de listView

I am stuck in this hashmap problem when copying the key. (Android)

Initially I had a xml file similar to this

<many_people>
  <people>
    <id>1</id>
     <name>Johnson</name>
     <tel_num>12345678</tel_num>
  </people>
     .
     .
     .
</many_people>

so i created arraylist with hashmap to store the whole xml

for (int i = 0; i < nl.getLength(); i++) {
        map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_TELNUM, parser.getValue(e, KEY_TELNUM));

        // adding HashList to ArrayList
        peopleList.add(map);
    }

I used onItemClickListener to capture the input from AutoCompleteTextView so that I can compare the input with the xml. The peopleList will later be used in listView.

For example: input - Johnson Compare with , if found then my list view will show Johnson's name with contact together. The problem is here, if I have 2 Johnson, the listView only read my first johnson in the xml. I do not know how to compare the input to the xml. Below is my code:

for (HashMap<String, String> map : peopleList) {
  if (input.equals(map.get(KEY_NAME))) {
      // At here I created another hashmap with copying the value
      map2.put(KEY_ID, map.get(KEY_ID));
      map2.put(KEY_NAME, map.get(KEY_NAME));
      map2.put(KEY_NAME, map.get(KEY_TELNUM));
      peopleList2.add(map2);
    }
}    

I successfully to show the first data in my listView but second and third data (same name as Johnson) failed. I not sure this is the way to do or not.

preguntado el 23 de diciembre de 12 a las 20:12

2 Respuestas

Aquí adentro:

for (HashMap<String, String> map : peopleList) {
  if (input.equals(map.get(KEY_NAME))) {
      // At here I created another hashmap with copying the value
      map2.put(KEY_ID, map.get(KEY_ID));
      map2.put(KEY_NAME, map.get(KEY_NAME));
      map2.put(KEY_NAME, map.get(KEY_TELNUM));
      peopleList2.add(map2);
    }
}    

At here I created another hashmap with copying the value

Well, if that is your whole FOR loop code, you definitely did not create una alternativa, map. You're just updating the same HashMap (map2) over and over and adding it to the list. Since you're using the same HashMap, you'll just update the same object over and over, ending with N (the number of matches) references to the same "person".

Like Michael said, there's no reason for this, you should create a Person class.

Respondido el 23 de diciembre de 12 a las 21:12

Ah somehow I completely missed the part where he was completely remaking the map every iteration of the first for loop... edited my answer and upvoted ours. - Michael

If two values with the same key are put in to a HashMap, the first is overwritten by the second. You need to make sure that each 'person' you enter in is unique, so that it does not overwrite a previous person. (Note: This is true, but it's not your problem)

Suggestion: Create a Person object, entering in the data for each person in to this new object. Then insert Person in to your hashmap. Remember to implement the equals y hashCode functions for your new object so that it will be efficiently entered in to the map.

Edit: I misread your original code. There are far worse problems. As dmon said, why would you create a new hashmap for each loop through the for loop, and then store only 4ish things in it? This is incredibly inefficient, and you should just get rid of the hashmap all together. Store the Person class in your list instead of a hashmap for each person...

Respondido el 23 de diciembre de 12 a las 21:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.