1

I have a ArrayList<HashMap<String, String>> placesListItems.

When I remove a map from placesListItems, the null map remains. So that my ListAdapter contains null list items.

for (HashMap<String, String> map : placesListItems) {
  for (Entry<String, String> entry : map.entrySet()) {
    for (int j = 0; j < duplicateList.size(); j++) {
      if (entry.getValue().equals(duplicateList.get(j))) {
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
          Entry<String, String> pairs = (Entry)iterator.next();
          System.out.println(pairs.getKey() + " = " + pairs.getValue());
          iterator.remove(); // avoids a ConcurrentModificationException
        }
      }
    }
  }
}     
ListAdapter adapter = new ItemAdapterHome(getApplicationContext, placesListItems);
lv.setAdapter(adapter); 

How can I solve this?

3
  • can you do something about your indentation ? Commented May 16, 2013 at 8:20
  • 1
    you are never removing anything from the placesListItems List. Commented May 16, 2013 at 8:23
  • remove the key value from the map, not the value. Commented May 16, 2013 at 8:31

2 Answers 2

3

What you need to do is add all empty maps in list and remove them all at the end.

List<HashMap<String, String>> mapsToRemove= new ArrayList<HashMap<String, String>>();//list in which maps to be removed will be added
for (HashMap<String, String> map : placesListItems)
   {
    for (Entry<String, String> entry : map.entrySet())
     {
      for (int j = 0; j < duplicateList.size(); j++) 
          {
        if(entry.getValue().equals(duplicateList.get(j)))
         {
          Iterator iterator = map.entrySet().iterator();
          while (iterator.hasNext()) 
               {
              Entry<String, String> pairs = (Entry)iterator.next();
              System.out.println(pairs.getKey() + " = " + pairs.getValue());
              iterator.remove(); // avoids a ConcurrentModificationException                   }
               }
          }
      }
      if(map.isEmpty){//after all above processing, check if map is empty
         mapsToRemove.add(map);//add map to be removed
      }
 }  
placesListItems.removeAll(mapsToRemove);//remove all empty maps


ListAdapter adapter = new ItemAdapterHome(getApplicationContext, placesListItems);
lv.setAdapter(adapter); 

You might need to change the logic a little based on your requirement.

Sign up to request clarification or add additional context in comments.

Comments

2

Your problem is that you are emptying maps instead of removing them from the list.

Try the following:

Iterator<Map<String, String>> iterator = placesListItems.iterator();
while (iterator.hasNext()) {
    Map<String, String> map = iterator.next();
    for (String value : map.values()) {
        if (duplicateList.contains(value)) { // You can iterate over duplicateList, but List.contains() is a nice shorthand.
            iterator.remove(); // Removing the map from placesListItems
            break; // There is no point iterating over other values of map
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.