-1

I followed this tutorial (http://www.vogella.com/tutorials/AndroidListView/article.html#expandablelistview) for an expandable list view, in which I enter different shop names, categorized by the part of town they are located:
screenshot

But there is obviously no need to have the same group several times (which is the case for Neukölln for example).

Here is what I tried to do:

SparseArray<Groups> groups = new SparseArray<Groups>();
DatabaseHandler db = new DatabaseHandler(this) ;
List<Shop> shops = db.getAllShops();

    Groups group = null ;
    String str = "null" ;

    int i = 0 ;
    for (Shop shop : shops){
        // if current shop's district differs from district of the shop of the last iteration
        if ( shop.getDistrict() != str ) {
            // making sure that empty group is not appended into groups in the first iteration
            if (group != null) {
                groups.append(i, group);
                i++;
            }
            // saving the district of the current shop
            str = shop.getDistrict();
            // creating a new group named after the current district
            group = new Groups(str);
        }
        // adding shop name to the current district
        group.subitems.add(shop.getName()) ;
    }
    // after last iteration the last group needs to be appended to the groups
    groups.append(i, group);

I hope you can understand the basic idea of my code. I sure it has to work out, because I went through it on paper and in theory it works. My first idea was that those if-statements are responsible, but I have no clue how to repair this.

CL.
  • 173,858
  • 17
  • 217
  • 259
VollNoob
  • 267
  • 4
  • 15
  • I basically used this tutorial on androidhive: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ In this tutorial the method is called getAllContacts() I just changed it a bit – VollNoob Nov 15 '14 at 22:32

1 Answers1

1

Your shop.getDistrict() != str statement will always return true, use String.equals instead. How do I compare strings in Java?

Community
  • 1
  • 1
Maxim Paliy
  • 376
  • 4
  • 7