Search code examples
androidlistandroid-listviewhashmapsimpleadapter

Simple Adapter to show part of List (made with Hashmap) by keyword


I have but of a problem. I've made a list as following:

List<Map<String, String>> ShopsList = new ArrayList<Map<String,String>>();
       private void initList() {
        // We add the cities
           ShopsList.add(createShop("Antwerpen", "Broer Bretel"));
           ShopsList.add(createShop("Antwerpen", "Caffènation"));
           ShopsList.add(createShop("Antwerpen", "Caffènation - Take Out Nation"));
           ShopsList.add(createShop("Antwerpen", "Coffeelabs"));
           ShopsList.add(createShop("Antwerpen", "De Dikke Kat"));
           ShopsList.add(createShop("Antwerpen", "Mlle Loustache")); 
           ShopsList.add(createShop("Berchem", "Broer Bretel"));
           ShopsList.add(createShop("Berchem", "Caffènation"));
           ShopsList.add(createShop("Berchem", "Caffènation - Take Out Nation"));

and

private HashMap<String, String> createShop(String key, String name) {
        HashMap<String, String> shop = new HashMap<String, String>();
        shop.put(key, name);
        return shop;
       }

So now I use SimpleAdapter to diplay this list in a Listview. But what I want is to be able to only show the data from the list with a specific keyword. So I do

ListView lv = (ListView) findViewById(R.id.listView);

        SimpleAdapter simpleAdpt = new SimpleAdapter(this, ShopsList, android.R.layout.simple_list_item_1,
                new String[] {"Antwerpen"}, new int[] {android.R.id.text1});

        lv.setAdapter(simpleAdpt);

When I do this, he only shows me the data with the right keyword, but adds the other entry's as empty. So when I ask for the second keyword, he first adds 6 empty places before displaying the right entry's.

How should I do this? I think I should add the locations of the entry's with the wanted keyword, but how do I retrieve these locations in an easy way?

Thanks!


Solution

  • edit:

    as it was pointed out, I mistaken SimpleAdapter with ArrayAdapter. Apologies and if you want to change your implementation to an ArrayAdapter (which is much simpler IMHO), the code is below.

    original:

    When filling up the android.R.id.text1 with the values, the ArrayAdapter just calls .toString() in each element from the List.

    There're several ways to accomplish what you want.

    • One of them would be to make a List<String> and just make the Strings as you want to be seen on the screen.

    • A valid more "O.O." approach is to create your own class.

    for example:

    public class Shop{
       private String city;
       private String name;
    
       public Shop(String city, String name){
           this.city = city;
           this.name = name;
       }
    
       @Override
       public String toString() {
           return city + " - " + name
       }
    }
    

    and then on your adapter you'll use a List<Shop> instead of a map. and by overriding the toString() method you can manipulate the text as you wish.

    • just to mention, another way of doing it could be a extending the ListAdapter class